Development involves a lot of processing of string, familiar with the common method of string, can improve the development efficiency.
1. Characters and strings, commonly used methods are:
- Public String (char[] value)
- Public String (char[] value, int offset, int count)
- public char charAt (int index)
- Public char[] ToCharArray ()
{ //Public char charAt (int index)//remove the character from the specified indexString str = "Hello"; Charc = Str.charat (1); System.out.println (c);} { //Public char[] ToCharArray ()//conversion of character arrays to stringsString str = "Hello"; Char[] Strinchararray =Str.tochararray (); for(CharItem:strinchararray) {System.out.print (item+ " "); } System.out.println ();} { //Public char[] ToCharArray ()//determines whether a given string consists of numbersString str = "13212a355565"; Char[] Strinchararray =Str.tochararray (); BooleanFlag =true; for(CharItem:strinchararray) { if(Item < ' 0 ' | | item > ' 9 ') {flag=false; Break; }} System.out.print (str+ ": "); if(flag) {System.out.println ("All made up of numbers!"); } Else{System.out.println ("Not all numbers!"); }}
2. Byte and string
- String (byte[] bytes)
- String (byte[] bytes, int offset, int length)
- Public byte[] GetBytes ()
- Public byte[] GetBytes (Charset Charset)
// Public byte[] GetBytes () // converting a string to a byte array String str = "Hello world"; byte [] strinbytes = str.getbytes (); for (byte item:strinbytes) { System.out.println ((int) item);}
3. String comparisons
- public boolean equals (Object anobject)
- public boolean equalsignorecase (String anotherstring)
String str1 = "Hello"= "Hello"; System.out.println (Str1.equals (str2)); // false System.out.println (Str1.equalsignorecase (str2)); // true
4. String Lookup
- Public Boolean contains (Charsequence s)
- public int indexOf (String str)
- public int indexOf (String str, int fromIndex)
- public int lastIndexOf (String str)
- public int lastIndexOf (String str, int fromIndex)
- Boolean startsWith (String prefix)
- public boolean startsWith (String prefix, int toffset)
- public boolean endsWith (String suffix)
String str = "Hello world"; System.out.println (Str.indexof ("World")); System.out.println (Str.indexof ("L")); System.out.println (Str.indexof ("L", 5)); System.out.println (Str.lastindexof ("L"));
Common methods of String in Java