I. Overview
View Java source code you can find that the string class is the final type, that is, string cannot be inherited. A string is a special object that cannot be changed once it is initialized, in "abc"
which case it creates a string object in memory and can be used to String s = "abc";
create a string object. You can also String s = new String("abc");
create a string object in the same way, but the difference between the two is that the former creates only one object in memory, and the latter creates two objects in memory, usually using the former to create the string object. It is also worth noting that the String s = new String();
default constructor of a string is actually creating an empty string, which is equivalent to the String s = "";
effect.
The following is a simple example of how String
objects are stored in memory.
"abc"new String("abc""abc";System.out.print((s1==s2)+"/"+(s1==s3));
The output of the above program segment is:
false/true
When the program is created, it creates an s1
area in memory "abc"
and points to the s1
address of the area, and when it is created, s2
two zones are created in memory, but the "abc"
zone already exists and is not created again; s3
“abc”
already exists in memory and is not created again. java
in String
fact, the object in the design mode is used to share the same string content.
Second, the common function of string to get the function
1 int length()
. Gets the length of the string.
2 char charAt(int index)
. Gets the character of the character index subscript position.
3, int indexOf(int ch)
get the first occurrence of the string in the position of CH, no return-1 found.
4, int indexOf(int ch, int fromIndex)
starting from the Fromindex position to obtain the position of CH (at this time the position or from the beginning of the calculation), did not find the return-1.
5 int indexOf(String str)
. Gets the position of STR for the first time in the string, and no return-1 is found.
6, int lastIndexOf(int ch)
the last occurrence of the position of CH, no return-1 found.
7, int lastIndexOf(int ch, int fromIndex)
starting from the Fromindex position to obtain the last position of CH (at this point or from the beginning of the calculation), did not find the return-1.
8 int lastIndexOf(String str)
. Gets the position of the last occurrence of Str in the string, no return-1 found.
Judge
1 boolean contains(CharSequence chs)
. Determine if CHS (which can be a string) appears in a string.
2, boolean isEmpty()
whether it is empty, and length() == 0
equivalent.
3, boolean startsWith(String str)
whether to str
start.
4, boolean endsWith(String str)
whether to str
end.
5, boolean equals(String str)
whether and STR content is the same.
6, boolean equalsIgnoreCase(String str)
ignore the case to determine whether the content is the same.
Transformation
1, new String(char[] chs)
constructor, to replace the char
array with chs
a string.
2, the new String(char[] chs, int offset, int count)
constructor converts the char
chs
characters in the array offset
starting from the subscript to a count
string.
3, static String copyValueOf(char[] chs)
function is to char
chs
return the converted string according to the array.
4 static String copyValueOf(char[] chs, int offset, int count)
. The function is char
chs
offset
count
converted to a string based on the characters starting from the subscript in the array.
5, static String valueOf(char[] chs)
and the copyValueOf(char[] chs)
same function.
6 static String valueOf(int v)/static String valueOf(long v)/...
. Convert other types of variables to string types.
7, char[] toCharArray()
the string is converted to a character array.
8 byte[] getBytes()
. Convert the string to a byte array.
Other common functions
1, String replace(char oldChar, char newChar)
oldChar
replace newChar
with and then return the replaced string.
2, String replace(CharSequence target, CharSequence replacement)
target
replace replacement
with and return the replaced string.
3 String[] split(String regex)
. Split this string according to the match of the given regular expression.
4 String substring(int begin)
. Gets begin
the string substring at the beginning of the position to the end.
5 String substring(int begin, int end)
. Gets [begin, end)
The string substring of the end.
6 String toUpperCase()
. Convert to uppercase string.
7 String toLowerCase()
. Convert to lowercase string.
8, String trim()
Remove the excess space.
9, int compareTo(String str)
and str
comparison, if the number is less than the return negative, if it is equal to return 0, if greater than return positive. The size of the string is based on the dictionary order.
Iii. Examples of string applications
1, simulation trim()
function, the function is very simple, is to remove the continuous white space can be, note that there is no space and all spaces and empty strings, the example code is as follows:
publicmyTrim(String str) { int0, end = str.length()-1; while‘ ‘) start ++; while‘ ‘) end --; return str.substring(start, end+1);}
2, the string reversal, "abcde"
is about "edcba"
to change, the step is to first convert the string into a character array, then flip the character array, and finally the character array to construct the string. The sample code is as follows:
publicmyReverse(String str) { char[] chs = str.toCharArray(); for(int start=0, end=chs.length-1; start < end; start++, end--) { char temp = chs[start]; chs[start] = chs[end]; chs[end] = temp; } returnnew String(chs);}
3, calculate "abkkcdkkefkkskk"
kk
The number of occurrences? indexOf(String, int)
the ability to find the number of occurrences of a string can be accomplished by using the method. The sample code is as follows:
publicintmyCount(String str, String substr) { int00; while((index=str.indexOf(substr, index))!=-1) { index += substr.length(); count ++; } return count;}
4. Take the longest common substring of two strings.
publicmyMaxSubstring(String s1, String s2) { for(int i=0; i<s2.length(); i++) { for(int j=0,k=s2.length()-i; k!=s2.length()+1; j++,k++) { String temp = s2.substring(j, k); if(s1.contains(temp)) { return temp; } } } return"";}
Four
StringBuffer
Introduction to use
To solve String
the non-changeable drawbacks, the emergence StringBuffer
of classes, StringBuffer
the characteristics are: variable length, you can manipulate multiple data types, such as the use of append
functions to add integer, floating point, character, Boolean and other data, the end can use the toString()
method return String
type. StringBuffer
is also final
a class of type.
Add Features
1, StringBuffer append(对象)
here can add general types of objects, such as Integer, float, Boolean and so on. The last return StringBuffer
itself, the this
pointer, is called the method call chain.
2 StringBuffer insert(int offset, 对象)
. You can insert a data object at the specified location, and the data after that position is postponed.
modifying features
1 StringBuffer replace(int start, int end, String str)
[start, end)
. Replace the segment with the str
.
2 void setCharAt(int index, char ch)
index
. Change the character of the position to ch
.
3 StringBuffer reverse()
. Reverse the string.
Remove Features
1 StringBuffer delete(int start, int end)
. Delete [start, end)
The character contents of the segment.
2 StingBuffer deleteCharAt(int index, char ch)
. Delete index
the character of the position.
3, delete(0, str.length())
can be used to clear the buffer.
Other features
1, the content of the void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
StringBuffer
Middle [srcBegin, srcEnd)
section is stored in the dst
dstBegin
location.
Wu, StringBuilder
StringBuilder
and StringBuffer
have compatible APIs. Appears in the JDK1.5 version. The StringBuffer
biggest difference is the non- StringBuilder
thread synchronization, which StringBuffer
is thread synchronization.
So it is generally used in a single thread StringBuilder
, because it is fast. Multi-Threading is also available StringBuilder
, but locks are required.
VI. basic data type Object wrapper class
Basic data Types |
class |
Basic data Types |
class |
Byte |
Byte |
Float |
Float |
Short |
Short |
Double |
Double |
Int |
Integer |
Char |
Character |
Long |
Long |
Boolean |
Boolean |
Basic data class to String
The basic data type goes to a string in a way:
基本数据类型+""; 或者 String.valueOf(); 或者 Integer.toString();等
The string to the base data type has the following example:
Integer.parseInt("123"// "123"转为123Long.parseLong("111"// "111"转为111的长整型Boolean.parseBoolean("true"// "true"转为trueInteger.parseInt("3c"16//按照16进制将"3c"转为整数,也就是60
Character
There is no parseCharacter
method.
VII. basic data type Object wrapper class new features
The new feature in the JDK1.5 version is that objects can be directly operated between values. As follows:
4// 直接将4赋值给整型对象x,称为自动装箱,等同new Integer(4)2//直接运算,x + 2自动拆箱,然后再将和自动装箱,并赋值给xnew2//x = x + 2的具体过程。
Integer
is int
more than one value null
. There are some of the following features:
128128127127;System.out.println(m == n);System.out.println(a == b);
The result is as follows, because when the value of integer is within the range of byte, if the value already exists in memory, the integer object is no longer created, but it points directly to the existing area.
falsetrue
Black Horse Programmer-string Series Introduction