Java string functions

Source: Internet
Author: User

Java strings are also a series of characters. However, unlike many other computer languages that process strings as character arrays, Java treats strings as String objects. Using strings as built-in object processing allows Java to provide a wealth of features to facilitate string processing. The following are some frequently used functions and their related descriptions.
String Functions
1) substring ()
It has two forms: String substring (int startIndex)
The second type is: String substring (int startIndex, int endIndex)
2) concat () connects two strings
Example: String s = "Welcome ";
String t = s. concat ("AnHui ");
3) replace ()
It can be in two forms. The first form is to replace all the places where a character appears in the call string with one character. The form is as follows:
String replace (char original, char replacement)
Example: String s = "Hello". replace ('l', 'w ');
The second form is to replace another character sequence with one character sequence. The form is as follows:
String replace (CharSequence original, CharSequence replacement)
4) trim () removes spaces at the beginning and end
5) convert valueOf () to a string
6) convert toLowerCase () to lowercase
7) convert toUpperCase () to uppercase
8) length () gets the length of a string
For example, char chars [] = {'A', 'B'. 'C '};
String s = new String (chars );
Int len = s. length ();
9) charAt () Intercepts one character
Example: char ch;
Ch = "abc". charAt (1 );
The returned value is 'B'
10) getChars () Intercepts multiple characters
Void getChars (int sourceStart, int sourceEnd, char target [], int targetStart)
SourceStart specifies the subscript of the starting character of the substring.
SourceEnd specifies the subscript of the next character after the substring ends. Therefore, the substring contains characters from sourceStart to sourceEnd-1.
Target specifies the array of characters to receive
Start copying the sub-string value in targetStart target
Example: String s = "this is a demo of the getChars method .";
Char buf [] = new char [20];
S. getChars (10, 14, buf, 0 );
11) getBytes ()
One way to replace getChars () is to store characters in byte arrays, that is, getBytes ()
Example:
String s = "Hello! Hello !";
Byte [] bytes = s. getBytes ();
12) toCharArray ()
Example:
String s = "Hello! Hello !";
Char [] ss = s. toCharArray ();
13) equals () and inclusignorecase () compare two strings
14) regionMatches () is used to compare a specific region in a string with another specific region. It has an overloaded form that allows case-insensitive comparisons.
Boolean regionMatches (int startIndex, String str2, int str2StartIndex, int numChars)
Boolean regionMatches (boolean ignoreCase, int startIndex, String
Str2, int str2StartIndex, int numChars)
15) startsWith () and endsWith ()
The startsWith () method determines whether to start with a specific string.
The endWith () method determines whether to end with a specific string.
16) equals () and =
The equals () method compares characters in a string object.
= The operator compares whether two objects reference the same instance.
Example: String s1 = "Hello ";
String s2 = new String (s1 );
S1.eauals (s2); // true
S1 = s2; // false
17) Comparison string between compareTo () and compareToIgnoreCase ()
18) indexOf () and lastIndexOf ()
IndexOf () is used to locate the first occurrence of a character or substring.
LastIndexOf () is the last occurrence of a character or substring.
19) trim space function
Example: String t1 = "abc de ";
System. out. println (t1.trim (); // remove the space "abc de" at the beginning and end"

20) split string segmentation
String y = "abc, de, fg, hi, jk ";
String [] y1 = y. split (","); // truncates all strings. ","
For (int I = 0; I <y1.length; I ++ ){
System. out. print (y1 [I]); // output abcdefghijk
}
21) append add or insert a function
StringBuffer zz1 = new StringBuffer (z1); // append insert characters
Zz1.append ('|'). append ("hijk"). append ('/'). append ("lmn"). append ("opq ");
System. out. println ();
System. out. print (zz1); // output: abcdefg | hijk/lmnopq

StringBuffer Constructor
StringBuffer defines three constructors:
StringBuffer ()
StringBuffer (int size)
StringBuffer (String str)
StringBuffer (CharSequence chars)
The following are StringBuffer related functions:
1) length () and capacity ()
The current length of a StringBuffer can be obtained by the length () method, and the entire allocable space is obtained by the capacity () method.
2) ensureCapacity () sets the buffer size.
Void ensureCapacity (int capacity)
3) setLength (): Set the buffer length.
Void setLength (int len)
4) charAt () and setCharAt ()
Char charAt (int where)
Void setCharAt (int where, char ch)
5) getChars ()
Void getChars (int sourceStart, int sourceEnd, char target [], int targetStart)
6) append () indicates that the string of any data type is connected to the end of the called StringBuffer object.
For example, int a = 42;
StringBuffer sb = new StringBuffer (40 );
String s = sb. append ("a ="). append (a). append ("!"). ToString ();
6) insert () insert a string
StringBuffer insert (int index, String str)
StringBuffer insert (int index, char ch)
StringBuffer insert (int index, Object obj)
7) index specifies the subscript of the position in which the string is inserted into the StringBuffer object.
8) reverse () reverse the characters in the StringBuffer object
StringBuffer reverse ()
9) delete () and deleteCharAt () delete characters
StringBuffer delete (int startIndex, int endIndex)
StringBuffer deleteCharAt (int loc)
10) replace ()
StringBuffer replace (int startIndex, int endIndex, String str)
11) substring () truncation
String substring (int startIndex)
String substring (int startIndex, int endIndex)

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.