Common methods for Java strings and string buffers __java

Source: Internet
Author: User
common methods for Java strings and string buffers Strings String

a method of judging function

Sample

equals determines whether two strings have the same character sequence string str = "ABCDEF";
Boolean RESULT1 = Str.equals ("abcdef");

Boolean result2 = Str.equals ("ABCdef");

System.out.println ("(equals): Result1 =" + Result1 + ", result2 =" + result2);
Equalsignorecase is the same as equals, but ignores case result1 = str.equalsignorecase ("abcdef");

RESULT2 = Str.equalsignorecase ("ABCdef");

System.out.println ("(equalsignorecase): RESULT1 =" + Result1 + ", result2 =" + result2);
Determines whether the specified string result1 = Str.contains ("A") is included;
RESULT2 = Str.contains ("x");

System.out.println ("(contains): RESULT1 =" + Result1 + ", result2 =" + result2);
StartsWith determines whether to start with the specified string result1 = Str.startswith ("abc");
RESULT2 = Str.startswith ("def");

System.out.println ("(startswith): RESULT1 =" + Result1 + ", result2 =" + result2);
EndsWith to determine whether to end result1 = Str.endswith ("abc") with the specified string;
RESULT2 = Str.endswith ("def");

System.out.println ("(endsWith): RESULT1 =" + Result1 + ", result2 =" + result2); IsEmpty judgment string isNo null RESULT1 = Str.isempty ();
RESULT2 = "". IsEmpty ();

System.out.println ("(isempty): RESULT1 =" + Result1 + ", result2 =" + result2);
CompareTo according to the dictionary order, the Unicode value of two strings per character is judged by the dictionary order, larger than the specified string, returns a positive number, and returns a negative number, equal returns 0 int num1 = Str.compareto ("abcdef");
int num2 = Str.compareto ("Abcdee");
int num3 = Str.compareto ("abcdeg");

System.out.println ("(compareTo): NUM1 =" + Num1 + ", num2 =" + num2 + ", num3 =" + num3);
Comparetoignorecase ignores case, same as CompareTo NUM1 = Str.comparetoignorecase ("ABCdef"); System.out.println ("(comparetoignorecase): NUM1 =" + NUM1);

Run Results

methods that have access to functionality

Demo

Length of string
str = "abcdef";

System.out.println ("(length):" + str.length ());

CharAt obtains the specified position of the character
System.out.println ("(charAt):" + Str.charat (2));

IndexOf gets the first occurrence of the specified string, not finding the return-1
System.out.println ("(indexOf):" + str.indexof ("CD"));

LastIndexOf gets the position of the last occurrence of the specified string, no return-1
System.out.println ("(LastIndexOf):" + str.lastindexof ("de"));

Split based on the specified regular expression, the cutting string, returning the string array
string[] ss = "Abc|def|ghi". Split ("[|]");
System.out.println ("(split):" + arrays.tostring (ss));

Substring intercept string
System.out.println ("(substring):" + str.substring (2)); Intercept from the position of index 2, intercept to the last
System.out.println ("(substring):" + str.substring (2, 4))//intercept from index 2, intercept to index 4, excluding 4 (header not wrapped)

Run Results

a method with transformation function

Sample

GetBytes converts a string into a byte array string
str = "abcdef";
byte[] Bytebuf = Str.getbytes ();
System.out.println ("(getBytes):" + arrays.tostring (bytebuf));

ToCharArray converts a string into a character array
char[] charbuf = Str.tochararray ();
System.out.println ("(ToCharArray):" + arrays.tostring (charbuf));

valueof converts any value/object to a string
System.out.println ("(valueof):"
        + string.valueof (new char[] {);

toUpperCase converts characters to uppercase
System.out.println ("(touppercase):" + str.touppercase ());

toLowerCase converts the string to lowercase
System.out.println ("(tolowercase):" + "ABCdef". toLowerCase ());

Concat stitching two string
str = "AAA". Concat ("CCC");
System.out.println ("(concat):" + str);

Run Results

methods that have replacement functions

Sample

Replace replaces the specified character in the string with the new string
str = "T  abc abc   ";
System.out.println ("(replace):" + str.replace ("A", "X"));

Trim removes the whitespace at both ends of the string
System.out.println (Trim): + Str.trim ());

Run Results
String Buffer Stringbuffer/stringbuilder

Common Methods

Sample

StringBuffer sb = new StringBuffer ();

Gets the initial length of the StringBuffer buffer
System.out.println ("(capacity):" + sb.capacity ());

Append append string
sb.append ("abc");
System.out.println ("(Append):" + SB);

Length gets the actual length of the buffer
System.out.println ("(length):" + sb.length ());

Inserts the data into the buffer at the specified position
sb.insert (1, "XX");
System.out.println ("(insert):" + SB);

Deletecharat deletes the character
Sb.deletecharat (2) of the specified position;
System.out.println ("(Deletecharat):" + SB);

Delete Deletes the specified range of characters
Sb.delete (2, Sb.length ());
System.out.println ("(delete):" + SB);

Replace replacement buffer specified range of string
sb.replace (1, 2, "B");
System.out.println ("(replace):" + SB);

Reverse the string in the inverted buffer
sb.reverse ();
System.out.println ("(reverse):" + SB);

Substring intercepts the string, does not manipulate the original buffer, returns a new string
str = sb.substring (1, 2);
System.out.println ("(substring): SB =" + SB + ", str =" + str);

Run Results

The

stringbuffer and StringBuilder have the same method, except that StringBuffer is thread-Safe , while StringBuilder is thread-unsafe .

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.