Java based learning (e): String class, common string operations
A, String class, String
- String is constant, its value can not be changed after creation, not a variable length.
String str = "hahaha" ;
- If you do not initialize a string object, a variable does not point to any memory address, then calls will go wrong.
- It can be used to create an empty string null assignment.
String str = null ;
- Initialization string
·String str = "hahaha"
·String str = new String ("hahaha")
·string str = String.valueOf(其他任意类型)
(used to any other type of elementary data type is converted into a string) - With a character array initialization
*new String(char[])
*new String(char[],int,int)
(int, int character interception array of characters among the int1 to int2)
·String.valueOf(char[])
·String.valueOf(char[],int,int)
public class first {
public static void main(String[] args) {
char[] ch = {'h','a','h','a'};
String s1 = new String(ch);
String s2 = new String(ch,1,2);
String s3 = String.valueOf(ch);
String s4 = String.valueOf(ch,1,2);
System.out.println(s1+"\n"+s2+"\n"+s3+"\n"+s4);
}
}
//-------------输出---------------
haha
ah
haha
ah
Second, the common string manipulation: Stitching
- · "+" Plus sign is used to splice a plurality of strings, a string or must be noted that both sides of the String object. (Right operand is forcibly converted into String type).
· "+ =" That isstr += str1 => str = str + str1
.
·字符串.concat(字符串)
(Returns a new object, the object itself does not change the original, you must do the assignment operator). - Character length:
字符串.length()
public class first {
public static void main(String[] args) {
char[] ch = {'h','a','h','a'};
String s1 = new String(ch);
System.out.println(s1.length());
}
}
Third, the common string operations: the index of the string
- the charAt () Method:
UsecharAt(int index)
method to obtain a character string specified index, int value for the parameter into the method, a first string index value 0, the character is the last character index length minus 1, returns to a char type.
public class first {
public static void main(String[] args) {
String s1 = "hahehrht";
System.out.println(s1.charAt(1));
}
}
//-----------输出-------------
a
- the indexOf () method:
using the indexOf () method may return into the reference characters in the specified String object first occurrence index position, -1 on failure.
indexOf () Overloaded there are four:
method | Functional Description |
---|---|
indexOf(int ch) | Returns the index into the reference position of the first occurrence of the character in the specified String object, into the reference to Unicode character encoding |
indexOf(int ch , int fromIndex) | fromIndex start the search from the reference index, the index returned into the position reference character first appeared in the specified String object, into the reference to Unicode character encoding |
indexOf(String str) | Returns the index position of the first reference string appears in the specified String object |
indexOf(String str , int fromIdex) | Search from the reference index fromIndex, returns the index into the reference position of the first occurrence of the string in the specified String object |
Fourth, the common string manipulation: the replacement string
method | Functional Description |
---|---|
replace(char old , char new) | Replace the old new, note the string old will all be replaced by new, support for regular expressions |
replaceAll(String regex , String replacement) | From the top of the string to start the search until the tail, are matched to the characters, strings, regular expressions will be replaced |
replaceFirst(String regex , String replacement) | Replace begin the search string from the top until the tail, are matched to the characters, strings, regular expressions, only the first match |
Five common string operation: string interception
method | Functional Description |
---|---|
substring(int beginIdex) | Start from the parameters taken index position, until the end of the specified String object |
substring(int beginIdex , int endindex) | The reference index is intercepted from the start position, the position of the end endindex-1 (left and right open and closed) |
Common operating six string: string split
method | Functional Description |
---|---|
split(String regex) | The string into a string in accordance with a certain plan of an array can be "," delimiters may be a regular expression |
String[] str = str.split(",");
Seven common string operations: determine the contents of the string end to end, end to end to the string of blanks, the string case conversion
operating | method | Functional Description |
---|---|---|
Analyzing the content of the string end to end: | startsWith(String prefix) | Determining whether the character string beginning with a prefix |
startsWith(String prfix , int fromidex) | Determining whether the string index from the beginning with a prefix | |
endsWith(String suffix) | Determining whether the suffix string begins | |
String end to end to space: | trim() | Returns a string string trailing spaces removed |
String case conversion: | toLowerCase() | Each character in the specified object is converted into lowercase |
toUpperCase() | Each character in the given object is converted to uppercase |
Eight common string manipulation: string comparison
in Java, whether the use of "==" to compare the data are equal, and compare strings in double equal sign is a reference to the same memory address.
method | Functional Description |
---|---|
equals(String anotherString) | Comparison string of |
equealsIgnoreCase(String anotherString) | Compare strings, case-insensitive |
Common operating IX strings: other characters operations
method | Functional Description |
---|---|
contains(CharSequences) | Determining whether to include a String object into the reference string, it returns a Boolean value |
compareTo(String antherString) | In accordance with the difference code comparing ASCLL, then continue to the next if the same comparison, if the different return ASCLL |
hashCode() | Returns a hash value of the string |
toCharArray() | The method may be converted into a string of new character array |
toString() | Converted to a string |
Ten, formatted output
format(string str , Objetc...args)
The conversion operation | Explanation |
---|---|
%s | String type |
%c | Character Types |
%b | Boolean |
%d | Integer type (decimal) |
%x | Integer type (hexadecimal) |
%The | Integer type (octal) |
%f | Floating-point type |
%a | Hexadecimal floating-point type |
%e | Index type |
%g | General floating-point type |
%% | Percentage Type |
%n | Newline |
%tx | Time as Date Type |
E.g:
public class first {
public static void main(String[] args) {
String s1 = "hahehrht";
System.out.println(String.format("%s这是一个字符串",s1));
}
}
//--------------输出-------------
hahehrht这是一个字符串
format also provides time formatting operations: link