Common string methods in "Java" development

Source: Internet
Author: User

The functionality of the Java string can be said to be very powerful, and each of its methods is useful.

There are two kinds of string classes commonly used in Java strings, namely the string class and the StringBuffer class.

Sting class

The object of the string class is immutable.

Create a string
string () string (string str) string(char// with a character array to generate a String object string (char  intint// with the count character starting with the offset bit of the character array value, build a String object

Common methods
int length () String tolowercase () // returns the lowercase of the current string String toUpperCase ()// Returns the upper case of the current string char[] tochararray ()// Returns an array of strings for the current string String trim ()// Delete the front and back spaces of the current string and return a new string

Instance:

New String ("    Hello World    "); Str.length ();     // the length of the returned STR isstr.tolowercase (); // convert str to lowercase, "    Hello World    "str.touppercase ();   convert str to uppercase, "    HELLO World    "char[] Strchar = Str.tochararray ();   converts str to strchar character array Str.trim (); // Delete before and after the space, "Hello World"

Methods of comparative nature
BooleanRegionmatches (intToffset, String str,intOoffset,intLen//compares the Len characters starting with the toffset of this string and the Len characters starting with Str from Ooffset, and returns True (used to detect the number of occurrences of a character-string in the current string)BooleanRegionmatches (BooleanIgnoreCase,intToffset, String str,intOoffset,intLen//Ibid., IgnoreCase Determines whether case is ignored, ignorecase is true when case is ignoredString concat (String str)//returns the current string after the link to the STR stringintCompareTo (String str)//compares Unicode in the same position in a string, if two strings are equal to return 0, otherwise the current string is greater than STR returns the difference of the comparison characterintComparetoignorecase (String str)//Ignore case comparison, ibid.BooleanEquals (Object anobj)//compares the values of two objects for equality. Compares two string objects for equalityBooleanEqualsignorecase (String anotherstring)//ignores case, compares two string objects for equalityBooleanStartsWith (String prefix[,intToffset])//determines whether the current string starts with a parameter prefix from the Toffset expense, and the [] bracket indicates that it can be omittedBooleanEndswidth (String prefix[,intToffset])//determines whether the current string ends with a parameter prefix starting from Toffset

Find method
//character Ch Lookup, note is characterintIndexOf (intCh//find the first character from the front to the position where CH appears, not found return-1intIndexOf (intChintFromIndex)//starting from the Fromindex position to find the first character ch appears position, not found return-1intLastIndexOf (intCh//from the back forward to find the first character ch appears position, not found return-1intLastIndexOf (intChintFromIndex)//find the first character before and after the Fromindex position ch appears, not found return-1//substring STR lookupintIndexOf (String str)//finds the first occurrence of a substring from the beginning of the current string, not found return-1intIndexOf (String str,intFromIndex)//finds the first occurrence of a substring from the fromindex position of the current string, not found return-1intLastIndexOf (String str)//finds the first occurrence of a substring from the end of the current string, not found return-1intLastIndexOf (String str,intFromIndex)//finds the first occurrence of a substring from the fromindex position of the current string, not found return-1CharCharAt (intIndex//returns the character of the current string at the index position

Replacement method
 //  replace  String replace (char  Oldchar, // Span style= "color: #008000;" > Replaces all Oldcha characters r in the string with the Newchar character  string Replacefirst (string regex, string replacement) //  String ReplaceAll (string regex, string replacement) //  String substring (int  start[, //  String[] Split (String regex) //  Returns an array of strings that the current string is split through regular expressions  

Other methods

Convert digitized string to base type

 Public Static  byteParsebyte (String s)throwsNumberFormatException Public Static   ShortParseshort (String s)throwsNumberFormatException Public Static   Shortparseint (String s)throwsNumberFormatException Public Static  LongParselong (String s)throwsNumberFormatException Public Static  floatParsefloat (String s)throwsNumberFormatException Public Static  DoubleParsedouble (String s)throwsNumberFormatException

Examples of usage:

int a = Integer.parseint ("23");

Converting other types to strings

 Public Static String valueOf (int  N)publicstatic string valueOf (char[] data)  Public Static String valueOf (Object obj)  Public Static String copyvalueof (char[] data) is equivalent to valueof (char[] data)

Examples of usage:

String.valueof (334);

StringBuffer class
StringBuffer ()//Create an empty StringBuffer objectStringBuffer (intLength//Create a StringBuffer object of lengthStringBuffer (String str)//Create a str string StringBuffer objectstringbuffer append (Object obj)//to add an object obj to the StringBuffer objectStringBuffer Insert (intPosition, Object obj)//inserts the object obj into the position position in the StringBuffer objectStringBuffer Setcharat (intPositionCharCh//Replace the position position in the StringBuffer object with the character chStringBuffer Deletecharat (intPosition//remove characters from the position positionStringBuffer Replace (intStartintEnd, String str)//replaces the position of start to end-1 in the StringBuffer object with the string str

Common string methods in "Java" development

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.