Java string common functions

Source: Internet
Author: User

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

Related Article

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.