String总结-部分

来源:互联网 发布:淘宝秋季新款女装套装 编辑:程序博客网 时间:2024/05/16 01:42

Modifier and Type

Method and Description

 char

charAt(int index)
        
  Returns the char value at the specified index.

public class TestCharAt {

         public static void charAt(int index) {

           

         }

  

   public static void main(String[] args) {

            String a="sophie";

            char c=a.charAt(1);

            System.out.println(c);

   }

}                                                               //this method can be read as char at index

 int

codePointAt(int index)
        
Returns the character (Unicode code point) at the specified index.

public class Test2 { 

     

public static void main(String[] args) {

         String a="sophiea";

         int c=a.codePointAt(6);        //charAt is the same result 97

         System.out.println(c);

}

}

int

codePointBefore(int index)
          
Returns the character (Unicode code point) before the specified index

public class Test2 { 

     

      public static void main(String[] args) {

                String a="sophieaa";

                int c=a.codePointBefore(7);

                System.out.println(c);

      }

}                                                    //result is 97

int

codePointCount(int beginIndex, int endIndex)
          
Returns the number of Unicode code points in the specified text range of this String.

public class Test2 { 

     

      public static void main(String[] args) {

                String a="sophie";

                int c=a.codePointCount(0,0);

                System.out.println(c);

      }

}                                                        //result is 0

int

compareTo(String anotherString)
          
Compares two strings lexicographically. 字典顺序向量

public class Test2 { 

     

      public static void main(String[] args) {

                String a="sophie";

                int c=a.compareTo("howard");

                System.out.println(c);

      }

}

 int

compareToIgnoreCase(String str)
          
Compares two strings lexicographically, ignoring case differences.

 String

concat(String str)
         
 Concatenates the specified string to the end of this string.

public class Test2 { 

     

      public static void main(String[] args) {

                String a="sophie";

                String b="love";

                String c=b.concat(a);

                System.out.println(c);

      }

}                                                          //result is loves

boolean

contains(CharSequence s)
          
Returns true if and only if this string contains the specified sequence of char values.

public class Test2 { 

     

      public static void main(String[] args) {

                String a="good study";

                boolean b=a.contains(" ");

                System.out.println(b);

      }

}                                                          //result is true

 boolean

contentEquals(CharSequence cs)
         
 Compares this string to the specified CharSequence.

public class Test2 { 

     

      public static void main(String[] args) {

                String a="good study";

                boolean b=a.contentEquals("good study");

                System.out.println(b);

      }

}

static String

copyValueOf(char[] data)
          Returns a String that represents the character sequence in the array specified.       
?

 

boolean

endsWith(String suffix)
          Tests if this string ends with the specified suffix.

public class Test2 { 

 

      public static void main(String[] args) {

                String a="good";

                boolean b=a.endsWith("od");

                System.out.println(b);

      }

}                                                                   //true

 

int

indexOf(int ch, int fromIndex)
          Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

 int

indexOf(String str)
          Returns the index within this string of the first occurrence of the specified substring.

 int

indexOf(String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

public class Test2 { 

 

      public static void main(String[] args) {

                String a="good day today";

                int b=a.indexOf('t',5);

                System.out.println(b);

      }

}

 

public class Test2 {

 

       public static void main(String[] args) {

                  String a="good day today";

                  int b=a.indexOf("today");

                  System.out.println(b);

       }

}

public class Test2 { 

 

      public static void main(String[] args) {

                String a="good day today";

                int b=a.indexOf("day",9);

                System.out.println(b);

      }

}

             

public class Test2 { 

 

      public static void main(String[] args) {

                String a="good day today";

                String b=a.toUpperCase();

                String c=b.toLowerCase();

                System.out.println(b);

                System.out.println(c);

      }                                       //GOOD DAY TODAY good day today

}

 

 String

substring(int beginIndex)
          
Returns a new string that is a substring of this string.