Common methods in String

Source: Internet
Author: User

Common methods of String in Java
1 length () Lengths of string
   Example: Char chars[]={' A ', ' B '. ' C '};
string S=new string (chars);
int len=s.length ();

2. CharAt () intercepts a character
   Example: Char ch;
ch= "abc". CHARAT (1); return ' B '

3. GetChars () intercepts multiple characters
  void getChars (int sourcestart,int sourceend,char target[],int targetstart)
SOURCESTART Specifies the subscript for the start character of the substring, and sourceend specifies the subscript for the next character after the substring ends. Therefore, the substring contains characters from Sourcestart to SourceEnd-1. The array of the received characters is specified by Target, and the subscript value starting to copy the substring in target is targetstart.
Example: String s= "This is a demo of the GetChars method.";
Char buf[]=new char[20];
S.getchars (10,14,buf,0);

4, GetBytes ()
   one way to replace GetChars () is to store characters in a byte array, which is GetBytes ().

5, ToCharArray ()

6, Equals () and equalsignorecase () compare two strings

7, Regionmatches () is used to compare a particular area of a string with another specific area, and it has an overloaded form that allows the case to be ignored in comparisons.
 Boolean regionmatches (int startindex,string str2,int str2startindex,int numChars)
Boolean regionmatches (Boolean ignorecase,int startindex,string str2,int str2startindex,int numChars)

8, StartsWith (), and EndsWith () StartsWith () methods determine whether to start with a specific string, and the Endwith () method determines whether to end with a specific string

9, Equals () and = =
The Equals () method compares the characters in a string object, and the = = operator Compares whether two objects refer to the same instance.
 Example: String s1= "Hello";
String S2=new string (S1);
S1.eauals (S2); True
S1==s2;//false

10, CompareTo () and comparetoignorecase () compare strings

  

11, IndexOf () and LastIndexOf ()
   indexOf () finds the first occurrence of a character or substring.
lastIndexOf () find the character or substring that is the last occurrence。

12, SUBSTRING ()it has two forms, the first of which is: String substring (int startIndex)
The second type is: String substring (int startindex,int endIndex)

13, concat () connect two strings

14, replace () replacement
  It has two forms, the first of which is replaced by a character in the invocation string where all occurrences of a character are in the form:
String replace (char Original,char replacement)
Example: String s= "Hello". Replace (' l ', ' w ');
The second form is to replace another sequence of characters with one character sequence, in the following form:
String replace (charsequence original,charsequence replacement)

15. Trim () Remove the start and end spaces

16, ValueOf () converted to string

17, toLowerCase () converted to lowercase

18. Convert toUpperCase () to uppercase

19, int hashcode (): Returns the hash table code of the current character

20, String (byte[]  bytes) Constructs a string object through a byte array

21, String (char[] chars) constructs a strings object from a char array

Constructor: String (char[] value)

Constructor: String (char[] value, int offset, int count): Converts a portion of an array into a string.

Constructor: String (byte[] value)

Constructor: String (byte[] value, int offset, int count): Converts a portion of an array into a string.


20. StringBuffer constructor function
   StringBuffer defines three constructors:
StringBuffer ()
stringbuffer (int size)
StringBuffer (String str)
StringBuffer (charsequence chars)
  
   (1), Length () and Capacity ()The current length of a stringbuffer can be obtained through the length () method, while the entire assignable space is obtained through the capacity () method.
  
   (2), ensurecapacity () sets the size of the buffer
    void ensurecapacity (int capacity)

   (3), SetLength () set the length of the buffer
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 () The string representation of any type of data can be concatenated to the end of the called StringBuffer object.
     example: int a=42;
StringBuffer sb=new StringBuffer (+);
String s=sb.append ("a="). Append (a). Append ("!"). ToString ();

   (7), 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 for inserting a string into the position of the StringBuffer object。

   (8), reverse () reverses the characters in the StringBuffer object
     StringBuffer reverse ()

   (9), delete (), and Deletecharat () remove characters
     stringbuffer Delete (int startindex,int endIndex)
stringbuffer deletecharat (int loc)

   (10), replace () replacement
    stringbuffer replace (int startindex,int endindex,string str)

   (11), substring () intercept sub-string
     String substring (int startIndex)
String substring (int startindex,int endIndex)

Example:
the methods given by//string can be called directly
Public class test{
Public static void Main (string[] args) {
String s = "Welcome to Java world!";
String S1 = "Sun Java";
System.out.println (S.startswith ("Welcome"));//String starting with Welcome
System.out.println (S.endswith ("World"));//String ending with world
String SL = S.tolowercase ();//convert all to lowercase
String SU = S.touppercase ();//convert all to uppercase
System.out.println (SL);
System.out.println (SU);
String B = s.substring (11);//starting from 11th bit
System.out.println (b);
String C = s.substring (8,11);//end at 11th place starting from eighth bit
System.out.println (c);
String d = S1.trim ();//Remove the trailing spaces
System.out.println (d);
String s2 = "I am a programmer, I am learning java";
String e = s2.replace ("I", "You");
System.out.println (e);
int f = 5;
String s3 = string.valueof (f);
System.out.println (S3);
String s4 = "I am, this, King";
string[] g = s4.split (",");
System.out.println (g[0]);
when converting a string to a base type, for example, Int,integer.praseint (string s)
When converting a base type to a string, for example, static string valueOf (int i)

int   parseint (String s)

byte Parsebyte (String s)

boolean Parseboolean (String s)

short Parseshort (String s)

long Parselong (String s)

double Parsedouble (String s)

Common methods in String

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.