Common Operations in the String class

Source: Internet
Author: User

        String s = "java01"      len =  }

          String s1 = "java01"       ch = s1.charAt(3       }

Int indexOf (int ch): returns the location where ch first appears in the string.

Int indexOf (int ch, int fromIndex): searches for the position specified by fromIndex to obtain the position where ch appears in the string.

Int indexOf (String str): returns the position where str first appears in the String.
Int indexOf (String str, int fromIndex): searches for the position specified by fromIndex to obtain the position where str appears in the String.

        String s = "javascript"      x1 = s.indexOf('s'      x2 = s.indexOf('a',2      x3 = s.indexOf("script"      x4 = s.indexOf("script",2     System.out.println("x1="+x1+",x2="+x2+",x3="+x3+",x4="+ }

        String s = "javascript"      x = s.lastIndexOf('a'     System.out.println("x="+}

        String s1 = "javascript"     String s2 = s1.substring(4     String s3 = s1.substring(0,5     System.out.println("s2="+s2+",s3="+ }

 

 

 

Stirng (char [], fffset, count): converts a portion of the character array into a string.

Static String copyValueOf (char []);
Static String copyValueOf (char [], int offset, int count); (offset indicates the start position and count indicates the number. If the length of the character array is exceeded, the character String is out of bounds)

// ValueOf this method can be used not only to convert character arrays into strings, but also to convert other basic data types into strings.
Static String valueOf (char []);
Static String valueOf (char [] data, int offset, int count)

        [] arr ={'j','a','v','a','s','c','r','i','p','t'          String s1 =      String s2 =  String(arr,2,7     System.out.println("s1="+s1+",s2="+             String s3 =     String s4 = String.copyValueOf(arr, 2, 7     System.out.println("s3="+s3+", s4="+ }

 

        String s = "javascript"     [] arr =     (   }

 

Stirng (byte [], fffset, count): convert part of the byte array

        [] arr ={'j','a','v','a','s','c','r','i','p','t'     String s1 =      String s2 =  String(arr,5,6     System.out.println("s1="+s1+",s2="+ }

        String s1 = "javascript"     [] arr =        }

 

Static String valueOf (int)

Static String valueOf (double)

3 + ""; String. valueOf (3); // This is the same

 

        String s1 = "javascript"     String s2 = s1.toUpperCase();     String s3 = s2.toLowerCase();     System.out.println("s2="+s2+",s3="+ }

 

 

 

Judgment:

Special: indexOf (str) Can index the location where str appears for the first time. If-1 is returned, it indicates that the str does not exist in the string. Therefore, it can be used to determine whether the specified method contains the str. This method can be used to determine whether or not the str method exists and obtain the position where it appears.
It is used only for determining the location with contains. You must determine the location and use indexOf

        String s = "javascript"      b = s.contains("java"  }

 

        String s = " ";      b =  }

 

        String s = "Array.java"      b1 = s.startsWith("Array");      b2 =s.endsWith(".java");     System.out.println("b1="+b1+",b2="+ }

 

        String s1 = "javascript"     String s2 = "javascript"     String s3 = "html+CSS"      b1 =      b2 =     System.out.println("b1="+b1+",b2="+ }

 

Comparison between objects uses the compareTo method, while comparison between basic data types uses operators such as greater than signs, smaller than signs, and equal signs.

        String s1 = "javascript"     String s2 = "javascript"     String s3 = "java"      b1 =      b2 =      b3 =     System.out.println("b1="+b1+",b2="+b2+",b3="+ }

 

Other common methods:


String replace (oldchar, newchar); replace one character
String replace (CharSequence target, CharSequence replacement); can replace a String

        String s = "javascript"     String s1 = s.replace('a', 'b'     String s2 = s.replace("java", "php"     System.out.println("s1="+s1+",s2="+ }

 

        String s = "java,javascript,php"     String[] ss = s.split(",");        }

 

        String s = "   javascript     "     String ss  =   }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.