Today, there are a lot of string problems in the written test, such as substring and indexof. Haha, I think it's okay, but I encountered a Concat, which bothered me for a while, I have never used this before. I came back and saw it! Hey!
String A = "ABC ";
String B = "def ";
A. Concat (B );
System. Out. println ();
String S = "hello ";
System. Out. println ("cares". Concat (s ));
System. Out. println ("". Concat ("hello "));
System. Out. println ("to". Concat ("get"). Concat ("her "));
You can guess what the result is: ABC Careshello Hello Together I was surprised, so I read the Java API as follows: Public String Concat (string Str) { Int otherlen = Str. Length (); If (otherlen = 0) { Return this; } Char Buf [] = new char [count + otherlen]; Getchars (0, Count, Buf, 0 ); Str. getchars (0, otherlen, Buf, count ); Return new string (0, Count + otherlen, Buf ); } |
It turns out that:
String A = "ABC ";
String B = "def ";
A. Concat (B );
System. Out. println ();
System. Out. println ("ABC". Concat (B ));
The result is:
ABC
Abcdef
You can understand it without too much explanation. Hey, it's interesting.
Below are some common string text processing functions in Java.
① Java. Lang. String --> substring (INT indexid)/string substring (INT beginidex, int endindex)
Get the substring: "unhappy". substring (2) --> "happy" (truncates the substring starting from indexid to ending)
"Emptiness". substring (20) --> "" (return empty string)
"Hamburger". substring (4, 8) --> "urger"
"Hamburger". substring (4,20) --> "Java. Lang. stringindexoutofboundsexception" Exception
The same usage for Java. Lang. stringbuffer/stringbuilder!
② Java. Long. String. Concat
"Cares". Concat (s) --> "caress"
"". Concat ("hello") --> "hello"
"To". Concat ("get"). Concat ("her") --> "together"
This method is available only in the string class.
③ Java. Lang. String. charat (INT index): Char (obtain the corresponding letter based on Index)
"Hello". charat (4) --> 'O' may throw an exception indexoutofboundsexception.
④ Java. Lang. String. lenght () is nothing to say, but do not forget that the array object does not have this function.
⑤ Java. Lang. String. contentequals (stringbuffer compstr): Boolean
Returns true if the string to be compared is exactly the same as that in compstr.
⑥ Java. Lang. String. startswith (string prefix): Boolean
Java. Lang. String. endswith (string suffix): Boolean
Determines whether a string starts and ends with a string.
7. java. Lang. String. indexof (string Str)/lastindexof (string Str)
Obtain the position of the first/last matching Str
Using java. Lang. String. Split (string RegEx): String []
Use the regular expression RegEx to cut strings.
Using java. Lang. String. touppercase ()/tolowercase to convert all strings to uppercase/lowercase letters
Using java. Lang. String. tochararray (): Char [] splits the string into character arrays.
Required Public String intern () When and only when string1.equals (string2)
Required Public String replaceall (string RegEx, string replacement)/replacefirst (string oldstr, string newstr)
Replace string
Trim java. Lang. String. Trim () removes space characters at both ends of the string