Common character processing functions in java

Source: Internet
Author: User

Substring ()
It has two forms: String substring (int startIndex)
The second type is: String substring (int startIndex, int endIndex)

Concat () connects two strings

Replace () 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)

Trim () removes spaces at the beginning and end

ValueOf () to a string

ToLowerCase () to lowercase

ToUpperCase () to uppercase

Length () gets the length of a string
Example:
Char chars [] = {'a', 'B'. 'C '};
String s = new String (chars );
Int len = s. length ();

CharAt () intercepts one character
Example:
Char ch;
Ch = "abc". charAt (1 );
The returned value is 'B'

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 );

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 ();

ToCharArray ()
Example:
String s = "Hello! Hello !";
Char [] ss = s. toCharArray ();

Equals () and equalsIgnoreCase () are two strings.

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)

StartsWith () and endsWith ()
The startsWith () method determines whether to start with a specific string, and the endWith () method determines whether to end with a specific string.

Equals () and =
The equals () method compares the characters in a string object, and 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

CompareTo () and compareToIgnoreCase () comparison strings

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.

StringBuffer constructor
StringBuffer defines three constructors:
StringBuffer ()
StringBuffer (int size)
StringBuffer (String str)
StringBuffer (CharSequence chars)

The following are StringBuffer related functions:
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.

EnsureCapacity () sets the buffer size.
Void ensureCapacity (int capacity)

SetLength (): Set the buffer length.
Void setLength (int len)

CharAt () and setCharAt ()
Char charAt (int where)
Void setCharAt (int where, char ch)

GetChars ()
Void getChars (int sourceStart, int sourceEnd, char target [], int targetStart)

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 ();

Insert () inserts a string
StringBuffer insert (int index, String str)
StringBuffer insert (int index, char ch)
StringBuffer insert (int index, Object obj)
Index specifies the subscript of the position where the string is inserted into the StringBuffer object.

Reverse character in StringBuffer object by reverse ()
StringBuffer reverse ()

Delete () and deleteCharAt () delete characters
StringBuffer delete (int startIndex, int endIndex)
StringBuffer deleteCharAt (int loc)

Replace () replace
StringBuffer replace (int startIndex, int endIndex, String str)

Substring ()
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.