String

char charAt(int index)  

return the value at the index     

String s = "Hello world";
System.out.println(s.charAt(0));    //H
s = "中国";
System.out.println(s.charAt(1));   //

int compareTo(String other)  

returns a negative value if the string comes before other in dictionary order, a positive

value if the string comes after the other, or 0 if the strings are equal

            String s1 = new String("a");
        String s2 = new String("a");
        // compare the content instead of address
        System.out.println(s1.compareTo(s2));    // 0, 
        String s3 = new String("b");
        System.out.println(s1.compareTo(s3));    //-1    
View Code

boolean equals(Object other)

return true if string equals other, only true when other is a string and the content is equals string

boolean equalsIgnoreCase(String other)

return true if the string equals other,except the upper/lowercase distinction

boolean startsWith(String prefix)

boolean endsWith(String suffix)

return true if the string starts/ ends with prefix/suffix

int indexOf(String str)

int indexOf(String str, int fromIndex)

return the start_index of the fisrt substring equals to the string str, or -1 if str not in this string,

boolean contains(CharSequence str)

Returns true if and only if this string contains the specified str

String s1 = new String("_hello world hello java");
String s2 = "hello";
int index = s1.indexOf(s2);  // 1
int nextIndex = s1.indexOf(s2, index+1);  //13
View Code

int lastIndexof(str, int fromIndex)

               String s1 = new String("_hello world hello java");
        String s2 = "hello";
        int index = s1.lastIndexOf(s2);  // 13
        int nextIndex = s1.lastIndexOf(s2, index - 1 );  //1            
View Code

int lengh()

String substring(int beginIndex, int endIndex)

String toLowerCase()

String toUpperCase()

String replace(CharSequence oldString, CharSequence newString)    //just take the CharSequence as String

String trim()    // eliminate all leading and tailing whitespace

                String s1 = new String("Abc");
        String upper = s1.toUpperCase();  // ABC
        String lower = s1.toLowerCase();  // abc
        int length = s1.length();   // 3
        String newString = s1.replace("b", "2");  // A2c
        
        String s2 = "   haha    ";
        String trim = s2.trim();   // haha
        String subStr = s1.substring(0, 2);  // Ab [0,2)        
View Code

 

String join(CharSequence delimiter, CharSequence...elements)  // static

return a new string joining all elements with the given delimiter

char[] toCharArray()

Converts this string to a new character array

String s1 = new String("中国");
String s2 = String.join("#", "a","b", "c"); //  a#b#c
char[] arr =  s1.toCharArray();    // [中, 国]
View Code

static String format(String format, Object... args)

Returns a formatted string using the specified format string and arguments.

Format String Syntax google

all the method above return the new String, cause the String is unmutable

String replaceAll (String regex, String replacement)     // regex 正则语法

String[] split(String regex)

Splits this string around matches of the given regular expression.

boolean matches(String regex)

Tells whether or not this string matches the given regular expression.

                String regex = "hello";
        String s = "hello jime sshello mm hellosdfs";
        String s2 = s.replaceAll(regex, "H");  // H jime ssH mm Hsdfs
        
        String s3 = "192.168.0.21";
        regex = "\\.";
        String[] strArr = s3.split(regex);  // [192, 168, 0, 21]

        s3 = "abc@xxx.com";
        regex = "[\\w]+@[\\w]+\\.com";  
        System.out.println(s3.matches(regex));   // true        
View Code

static String valueOf(int i);    // also can be float double byte...

Returns the string representation of the int argument.

String s = String.valueOf(100);   //100  数字转字符串

 

posted @ 2017-08-02 12:52  征程126  阅读(243)  评论(0编辑  收藏  举报