Some methods of String in java

Source: Internet
Author: User

1. public String (char [] c, begin, length ).
Starting from the subscript begin of character array c, convert the string array with length to a string.
Begin and length can be omitted, which converts character array c to a string. In addition, the character array can be changed to a byte array byte [] B.
  
Char [] c = new char [] {'J', 'y', '6', 'A', '4', 't', '9 '};
 
String s1 = new String (c );
 
String s = new String (c, 2, 3 );
 

System. out. println (s1 );
System. out. println (s );
2. public char [] toCharArray ().
Replace string with character array.


3. public char charAt (int subscript ).
Returns the character at the specified position in the string.
String s = "jkdfsdf ";
Char t = s. charAt (3 );

4. public byte [] getBytes ().
Converts a string to a byte array. The default output is an ASCII value. You can use the char type to forcibly convert the output byte.
String s = "sjdfsdf ";
Byte [] B = s. getBytes ();
5. public String trim ().
Removes spaces at both ends of the string.
String s = "skkgnsdfsd ";
System. out. println (s. trim ());

6. public int indexOf (String s, int index ).

Find the location of the specified character after the specified position from the string. If no position is specified, start from scratch.

String s = "dgdgdg ";

Int n = s. indexOf ("t"); // search from the beginning

Int n1 = s. indexOf ("d", 3); // start from position 3

7. public String substring (int beginindex, int endindex ).

Truncates the specified string from the start position to the end position, and does not contain the end character. The end position can be omitted.

String s = "sdgsgghd ";

String s1 = s. substring (2, 4 );

String s2 = s. substring (2 );

8. public String [] split (String s ).

Splits a string by a specified character.

String s = "dfgdhdfgdrhrhgdt ";

String ss [] = s. split ("d ");

For (int I = 0; I <ss. length; I ++)

System. out. println (ss [I]);

9. public String toUpperCase ()./public String toLowerCase (). case-sensitive conversion.

String s = "dfgdhdfgdrhrhgdt ";

String s1 = s. toUpperCase (); // all uppercase characters

String s2 = s. toLowerCase (); // all lowercase characters

10. public boolean startsWith (String s)./public boolean endsWith (String s). Check whether the String starts/ends with a specified character.

String s = "dfdhffghrtgfjn mjg ";

Boolean t1 = s. startsWith ("e ");

Boolean t2 = s. endsWith ("h ");

11. Determine whether the strings are equal. Case Sensitive: equals (). Case Insensitive signorecase ().

String s = "dfgdghdf ";

String s1 = "sfsgsdu ";

S. equals (s1 );

12. public String replaceAll (String s, String s1). Replace s in the String with s1.

String s = "dfgdghdf ";

String s1 = s. replaceAll ("d", "f ");

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.