String常用方法(一)

来源:互联网 发布:dhcp服务器端口 编辑:程序博客网 时间:2024/06/06 09:48

常用的方法

public char charAt(int index) 返回字符串中第index个字符;
public int length() 返回字符串的长度;
public int indexOf(String str) 返回字符串中第一次出现str的位置;
public int indexOf(String str,int fromIndex) 返回字符串从fromIndex开始第一次出现str的位置;
public boolean equalsIgnoreCase(String another) 比较字符串与another是否一样(忽略大小写);
public String replace(char oldchar,char newChar) 在字符串中用newChar字符替换oldChar字符

public boolean startsWith(String prefix) 判断字符串是否以prefix字符串开头;
public boolean endsWith(String suffix) 判断一个字符串是否以suffix字符串结尾;
public String toUpperCase() 返回一个字符串为该字符串的大写形式;
public String toLowerCase() 返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)
返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex,int endIndex)
返回该字符串从beginIndex开始到endsIndex结尾的子字符串
public String trim()
返回该字符串去掉开头和结尾空格后的字符串
public String[] split(String regex)
将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

char[] toCharArray 将字符串转换成字符数组
String valueOf(Boolean b) 将布尔方法b的内容用字符串表示
String valueOf(char ch) 将字符ch的内容用字符串表示
String valueOf(int index) 将数字index的内容用字符串表示
String valueOf(long l) 将长整数字l的内容用字符串表示
char[] getBytes 将字符串转换成字符数组返回
char[] getBytes(String str) 将指定的字符串转成制服数组返回
String concat(String str) 将两字符串连接

String类中提供了丰富的用于操作字符串的方法。

int indexOf(String str)
该方法用于返回当给定字符串在当前字符串中的位置,若当前字符串不包含给定字符串则返回-1。
重载的方法
int indexOf(String str,int formIndex),从指定下标处(包含)查询子串,返回返回当给定字符串在当前字符串中的位置,若当前字符串不包含给定字符串则返回-1。
相关的方法
int lastIndexOf(String str),该方法用于返回子字符串在该字符串中最后一次出现的下标。

    public void testIndexOf(){        //          0123456789012345         String str="thinking in java";        //查找in        int index=str.indexOf("in");        System.out.println(index);//2        int index2=str.indexOf("ja");        int index3=str.indexOf("java");        System.out.println(index2);//12        System.out.println(index3);//12        if(str.indexOf("java")!=-1){            String str2=str.replace("ja","jaaa");//thinking in jaaava            System.out.println(str2);        }        //重载 indexOf(str,包含的起始下标),下标可越界        int index4=str.indexOf("ja",16);        System.out.println("重载的indexOf:"+index4);        //lastIndexOf()        int index5=str.lastIndexOf("i");        System.out.println("最后一个i的下标:"+index5);    }

String substring(int start,int end)
截取字符串,传入的两个参数分别为要截取边界的下标。
在java api 中,通常使用两个数字表示范围时,都是含头不含尾,即包含起始下标对应的内容,但不包含结束下标的处对应的内容。

public static void main(String[] args) {              //使用该方法截取域名                   String str="http://www.abcde.cn";        String name=str.substring(11,15);        System.out.println(name);        //重载  substring(int index):从该下标到结束        name=str.substring(11);        System.out.println(name);           //截取域名        int point=str.indexOf(".");        int end=str.lastIndexOf(".");        String name1=str.substring(point+1,end);        System.out.println(name1);    }    @Test    public void test01(){        String str2="http://www.abc.com.cn";        int start=str2.indexOf(".")+1;        int end=str2.indexOf(".",start);        String name=str2.substring(start,end);        System.out.println(name);    }

String trim()
去除一个字符串两边的空白字符(如空格,tab)

        String str="  hello         ";        String trim=str.trim();        System.out.println(trim);//hello

char charAt(int index)
返回当前字符串中指定位置的字符

//charAt:通过位置 获取内容String str="abcdef";char c=str.charAt(2);System.out.println(c);

boolean startWith(String str)
boolean endWith(String str)
判断当前字符串是否是以给定的字符串开始或结束的

public static void main(String[] args) {//使用这两个方法判断是否是图片               String str="zhangc.jpg";        boolean starts=str.startsWith("zh");        boolean end=str.endsWith(".jpg");        System.out.println(starts);        System.out.println(end);    }

String toUpperCase()
String toLowerCase()
将当前字符串中的英文部分转换为全大写或全小写

public static void main(String[] args) {        String str="Hellow World";        str=str.toUpperCase();        System.out.println(str);        str=str.toLowerCase();        System.out.println(str);    }

参考博客:
http://blog.csdn.net/kaishizhangcheng/article/details/52332543 http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html

原创粉丝点击