码迷,mamicode.com
首页 > 其他好文 > 详细

String类中常用的方法

时间:2015-10-26 11:48:57      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

String类中有许多方法和构造方法: 

一、常见的构造方法: 
1、public String() 
  String str=new String("abcdefg") 

2、public String(byte[] bytes):通过byte数组构造字符串对象 
     byte[]b={‘a‘,‘b‘,‘c‘,‘d‘}; 
     String t1=new String (b); 
     System.out.println(t1); 
输出结果:abcd 

3、public String(byte[] bytes,int offset, int length):bytes - 要解码为字符的 byte,offset - 要解码的第一个 byte 的索引,length - 要解码的 byte 数 
      byte[]b={‘a‘,‘b‘,‘c‘,‘d‘}; 
      String t3=new String(b,1,2); 
      System.out.println(t3); 
  输出结果:bc 

4、public String(char[] value):通过char数组构造字符串对象 
      char[]c={‘0‘,‘1‘,‘2‘,‘3‘}; 
      String t4=new String (c); 
      System.out.println(t4); 
  输出结果:0123 

5、public String(char[] value,int offset,int count):value - 作为字符源的数组,offset - 初始偏移量,count - 长度 
      char[]c={‘0‘,‘1‘,‘2‘,‘3‘}; 
      String t5=new String(c,1,3); 
      System.out.println(t5); 
输出结果:123 
  
二、常见的方法: 

   以字符串对象String str="abcdefg" 
             String str1="bcdefg" 为例 

1、public char charAt(int index):返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。 
  
      char str2=str.charAt(2); 
      System.out.println(str2); 
输出结果:c 

2、public int codePointAt(int index):返回指定索引处的字符(Unicode 代码点)。索引引用 char 值(Unicode 代码单元),其范围从 0 到 length() - 1。 

     int t=str.codePointAt(2); 
     System.out.println(t); 
输出结果:99 

3、public int codePointBefore(int index):返回指定索引之前的字符(Unicode 代码点),其范围从 1 到 length。 

     int t1=str.codePointBefore(2);     
     System.out.println(t1); 
输出结果:98 

4、public int codePointCount(int beginIndex,int endIndex):返回此 String 的指定文本范围中的 Unicode 代码点数,文本范围始于指定的 beginIndex到 endIndex - 1 处。 

    int t2=str.codePointCount(0,2); 
System.out.println(t2); 
  输出结果:2 

5、public int compareTo(String anotherString):按字典顺序比较两个字符串,该比较基于字符串中各个字符的 Unicode 值。 
     
     int t3=str.compareTo(str1); 
     System.out.println(t3); 
输出结果:-1 

6、public int compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写。 
7、public String concat(String str):将指定字符串连接到此字符串的结尾。如果参数字符串的长度为 0,则返回此 String 对象。 

    String str3=str.concat(str1); 
    System.out.println(str3); 
输出结果:abcdefgbcdefg 

8、public boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true,只要有包含,但是是要连续的包含,区分大小写。 
        
       boolean str4=str.contains(str1); 
       System.out.println(str4); 
输出结果:true 

9、public boolean contentEquals(CharSequence cs):将此字符串与指定的 CharSequence 比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true。 
        
       boolean s=str.contentEquals(str1); 
       System.out.println(s); 
输出结果:false 

10、public boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束,如果参数是空字符串,或者等于此 String 对象,则结果为 true。 
       
       boolean t=str.endsWith(str1); 
       System.out.println(t); 
输出结果:true 

11、public boolean equals(Object anObject):将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。 
      
      boolean t1= str.equals(str1); 
      System.out.println(t1); 
输出结果:false 

12、boolean equalsIgnoreCase(String anotherString):将此 String 与另一个 String 比较,不考虑大小写。 
13、static String format(String format,Object... args):使用指定的格式字符串和参数返回一个格式化字符串。 

       String s1=str.format( "%e", 2.22); 
       System.out.println(s1); 
输出结果:2.220000e+00 

14、public byte[] getBytes():默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中,所得 byte 数组。 

      byte[] bytes= str.getBytes(); 
      System.out.println(bytes); 
输出结果:[B@1ac3379 

15、public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin):要复制的第一个字符位于 srcBegin 处;要复制的最后一个字符位于 srcEnd-1 处。要复制到 dst 子数 

组中,从 dstBegin 处开始, 

     char t[]={‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘}; 
      str.getChars(1,3,t,1); 
      System.out.println(t); 
输出结果:kbcnop 

16、public int hashCode():返回此字符串的哈希码。 

      int x=str.hashCode(); 
      System.out.println(x); 
输出结果:-1206291356 

17、int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引,返回第一次出现该字符的索引。 

    int x1=str.indexOf(99); 
    System.out.println(x1); 
输出结果:2 

18、public int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 

       int x2=str.indexOf(99, 1); 
       System.out.println(x2); 
输出结果:2 

19、public int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。 
20、public int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 
21、public boolean isEmpty():当且仅当 length() 为 0 时返回 true。 
22、public int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。 
23、public int lastIndexOf(int ch,int fromIndex):返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 
24、public int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引。 
25、public int lastIndexOf(String st,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 
26、public int length():返回此字符串的长度。 
27、public String replace(char oldChar,char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 
       
      String s3= str.replace(str, str1); 
      System.out.println(s3); 
输出结果:bcdefg 

28、public boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始,如果参数是空字符串,或者等于此 String 对象,则返回 true。 
      
       boolean s5=str.startsWith(str1); 
       System.out.println(s5); 
输出结果:false 

29、public boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 

       boolean s6=str.startsWith(str1, 1); 
       System.out.println(s6); 
输出结果:true 

30、public String substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。 

       String s7=str.substring(5); 
       System.out.println(s7); 
输出结果:fg 

31、public String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串的一个子字符串。该子字符串从 beginIndex 处开始,直到索引 endIndex - 1 处。 
32、public String toLowerCase():将此 String 中的所有字符都转换为小写。 

    String str="ABCD"; 
    String s9=str.toLowerCase(); 
    System.out.println(s9); 
输出结果:abcd 

33、public String toUpperCase():将此 String 中的所有字符都转换为大写。 
34、public String trim():此字符串移除了前导和尾部空白的副本;如果没有前导和尾部空白,则返回此字符串。 

    String str=" abc " 
    String a2=str.trim(); 
    System.out.println(a2); 
输出结果:abc 

  
 


1 

 【新技术】现在最流行的java后台框架组合java springmvc mybaits mysql oracle html5 后台框架源码获取【下载地址】 

String类中常用的方法

标签:

原文地址:http://www.cnblogs.com/fhamdin24/p/4910465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!