Some methods of String in Java are parsed in depth

  • 2020-04-01 02:10:25
  • OfStack

1, public String(char[] c,begin,length).
Converts a length array of characters to a string, starting with the subscript begin of the character array c.
Begin and length can be omitted to convert an array of characters c to a string. Also: the character array can be changed to the 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. The out. Println (s1);
System. The out. Println (s);

2, public char[] toCharArray().
Replace the string with an array of characters.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/2013071811380320.png ">

3, public char charAt(int).
Returns the character at the specified position in the string.
String s = "JKDFSDF";
Char t = s.c harAt (3);

4, public byte[] getBytes().
Converts a string to a byte array whose default output is an ASCII value and whose output bytes can be cast by char. String s = "SJDFSDF";
Byte [] b = s.g etBytes ();

5. Public String trim().
Clear the Spaces on the left and right sides of the string.
String s = "skkgnsdfsd    ";
System. The out. Println (s.t rim ());

6. Public int indexOf(String s,int index)
Finds the position of the specified character in a string after the specified position. If you do not specify a location, start from scratch.
String s = "DGDGDG";
Int n = s.i ndexOf (" t "); // start from scratch
Int n1 = s.i ndexOf (" d ", 3); // start at location 3

7. Public String substring(int beginindex,int endindex)
Intercepts the specified string from the start position to the end position, without the end character. The end position can be omitted.
String s = "SDGSGGHD";
String s1 = s.s ubstring (2, 4);
String s2 = s.s ubstring (2);

8. Public String[] split(String s).
Splits a string by the specified character.
String s = "DFGDHDFGDRHRHGDT";
String ss [] = s.s plit (" d ");
For (int I = 0; I< Ss. Length; I++)
System. The out. Println (ss [I]);

Public String toUpperCase()./public String toLowerCase().
String s = "DFGDHDFGDRHRHGDT";
String s1 = s.t oUpperCase (); // characters are all uppercase
String s2 = s.t oLowerCase (); // characters are all lowercase

10, public Boolean startsWith(String s)./public Boolean endsWith(String s). Detects whether a string begins/ends with the specified character.
String s = "DFDHFFGHRTGFJN MJG";
Boolean t1 = s.s tartsWith (" e ");
Boolean t2 = s.e ndsWith (" h ");

To determine whether the strings are equal, case sensitive: equals (). Case insensitive equalsIgnoreCase().
String s = "DFGDGHDF";
String s1 = "sfsgsdu";
S.e quals (s1);

12. Public String replaceAll(String s,String s1). Replace all s in the string with s1.
String s = "DFGDGHDF";
String s1 = s.r eplaceAll (" d ", "f");


Related articles: