String的常用操作方法

来源:互联网 发布:php接口文档 编辑:程序博客网 时间:2024/05/13 13:07

2、String的常用操作方法

2.1、字符与字符串

       在String类中提供了以下的方法操作字符与字符串间的转换关系:
|-根据字符串中提供的索引找到指定位置的字符:public char charAt(int index)
|-将字符串变为字符数组:public char[] toCharArray()
|-将字符数组变为字符串:
|-将全部的字符数组变为String类型:public String(char[] value)
|-将部分的字符数组变为String类型:” public String(char[] value,int offset,int count)
范例:取出字符串中制定位置的字符
public class StringAPIDemo01
{
       public static void main(String args[])
       {
              String str = "hello" ;
              char c = str.charAt(2) ;
              System.out.println(c) ;
       }
}
范例:字符串 –-> 字符数组
public class StringAPIDemo02
{
       public static void main(String args[])
       {
              String str = "hello world !!!!!#@" ;
              char c[] = str.toCharArray() ;
              for (int i = 0 ; i < c.length ; i++ )
              {
                     System.out.print(c[i] + "、" ) ;
              }
              String str1 = new String(c) ;
              String str2 = new String (c,0,5) ;
              System.out.println("\n"+str1) ;
              System.out.println(str2) ;
       }
}

2.2、字节与字符串

       与字符数组的操作一致,一个字符串也可以变为字节数组,一个字节数组也可以变为字符串:
              |-String à字节数组:public byte[] getBytes()
        |-字节数组 àString
            |-全部:public String(byte[] bytes)
            |-部分:public String(byte[] bytes,int offset,int length)
范例:字节数组 à字符串
public class StringAPIDemo03
{
    public static void main(String args[])
    {
        String str = "hello world !!!!!#@" ;
        byte b[] = str.getBytes() ;  //将字符串变为字节数组
        String str1 = new String(b) ;
        String str2 = new String (b,0,5) ;
        System.out.println("\n"+str1) ;
        System.out.println(str2) ;
    }
}

2.3、判断是否以指定的字符串开头或结尾

       |-判断是否以指定的字符串开头:public boolean startsWith(String prefix)
    |-判断是否以指定的字符串结尾public boolean endsWith(String suffix):
范例:验证操作
public class StringAPIDemo04
{
    public static void main(String args[])
    {
        String str = "**hello world ##" ;
        System.out.println(str.startsWith("**")) ;
        System.out.println(str.endsWith("##")) ;
    }
}

2.4、替换操作

       使用以下的方法可以完成替换的操作:
        |-public String replaceAll(String regex,String replacement)
范例:替换内容
public class StringAPIDemo05
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String newStr = str.replaceAll("l","x") ;
        System.out.println(newStr) ;
    }
}

2.5、字符串的截取

       使用以下两种方法可以完成字符串的截取操作:
              |-全部截取:public String substring(int beginIndex)
        |-部分截取:public String substring(int beginIndex,int endIndex)
范例:验证操作
public class StringAPIDemo06
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String sub1 = str.substring(6) ;   //从第六个字符开始截取
        String sub2 = str.substring(0,5) ;  //从第一个字符截取到第五个字符
        System.out.println(sub1) ;
        System.out.println(sub2) ;
    }
}

2.6、字符串的拆分

       可以将字符串按照指定的内容进行拆分操作:
              |-public String[] split(String regex)
范例:拆分字符串
public class StringAPIDemo07
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String s[] = str.split(" ") ;
        for (String st :s)
        {
            System.out.println(st) ;
        }
    }
}

2.7、字符串查找

       如果需要在一个字符串中查找是否存在指定的内容,可以使用以下的两个方法:
              |-取得指定字符串的位置:public int indexOf(int ch),public int indexOf(int ch, int fromIndex)
            |-此方法返回int型数据,如果查到了则返回位置,查不到,返回-1 ;
        |-直接查找:public boolean contains(String s)
范例:查找操作
public class StringAPIDemo08
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.contains("hello")) ;    //ture
        System.out.println(str.contains("aust")) ;      //false
    }
}
范例:查找位置
public class StringAPIDemo09
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.indexOf("hello")) ;
        System.out.println(str.indexOf("aust")) ;
        if((str.indexOf("aust")) != -1)
        {
            System.out.println("查找到所需的内容。");
        }
    }
}
范例:指定查找的开始位置
public class StringAPIDemo10
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.indexOf("hello")) ;
        System.out.println(str.indexOf("hello " , 6)) ;
    }
}

2.8、字符串的其他操作

       去掉左右空格:public String trim()
       取的字符串长度:public int length()
       转大写:public String toUpperCase()
       转小写:public String toLowerCase()
范例:
public class StringAPIDemo11
{
    public static void main(String args[])
    {
        String str = " hello world " ;
        System.out.println(str.trim()) ;
        System.out.println(str.trim().length());
        System.out.println(str.trim().toUpperCase()) ;
    }
}