Next to the article on string, you have to know something! The last time I said some of the concepts and considerations of string, this article is to sort out some of the usual operations. 1, String condition operation
String comparisons are often seen in programs, except for the most frequent comparisons of content, and other comparison methods 1.1 Compare string contents for equality
String str = "Hello";
System.out.println (str.equals ("Hello"));
Output: True
1.2 Compare string contents for equality (ignore case)
String str = "Hello";
System.out.println (Str.equals ("Hello"));
System.out.println (Str.equalsignorecase ("Hello"));
Output:
False
True 1.3 Determines whether a string is equal to a given regular expression
String str = "Hello";
System.out.println (Str.matches ("\\w+"));
Output is: true
It should be noted here that matches (String regex), the argument must be a regular expression
1.4 Determines whether the string starts with the specified prefix, or ends with the suffix end specified:
String str = "Hello";
System.out.println (Str.endswith ("O"));
System.out.println (Str.endswith ("Lo"));
System.out.println (Str.endswith ("Llo"));
System.out.println (Str.endswith ("Lla"));
The results are:
True
True
True
False
Specify prefix start:
String str= "Hello";
System.out.println (Str.startswith ("H"));
System.out.println (Str.startswith ("he"));
System.out.println (Str.startswith ("hel"));
System.out.println (Str.startswith ("hol"));
The results are:
True
True
True
False starts with the specified prefix at the specified index
String str= "Hello";
System.out.println (Str.startswith ("L", 0));
System.out.println (Str.startswith ("L", 1));
System.out.println (Str.startswith ("L", 2));
System.out.println (Str.startswith ("Llo", 2));
System.out.println (Str.startswith ("Llo", 3));
The results are:
False
False
True
True
False 1.5 To determine the length of the string and whether the string is empty
String str1 = "Hello";
String str2 = "";
System.out.println (Str1.length ());
System.out.println (Str2.length ());
System.out.println (Str1.isempty ());
System.out.println (Str2.isempty ());
The results are:
5
0
False
True
It should be noted that the meaning of "" and "null" is completely different.
String str= "" is the case in memory:
String Str=null in memory this is the case
Because NULL does not have a corresponding heap memory space at all, a null pointer error occurs if the Lenth () and IsEmpty () methods are invoked:
Exception in thread "main" java.lang.NullPointerException
2, String cutting (string array), character array 2.1 string is cut into a string array form
public class test{public
static void main (string []args) {
string str= "Hello everyone I name is Simon";
String a[]=str.split ("");
for (int i=0;i<a.length;i++)
System.out.println (A[i]);
}
The results are:
Hello
Everyone
My
Name
Is
Simon
It is important to note that the split (String regex) argument must be a regular expression! 2.2 string split into character array form
String str= "Hello everyone I name is Simon";
Char A[]=str.tochararray ();
for (int i=0;i<a.length;i++)
System.out.println (A[i]);
The output is:
H
E
L
L
O
E
V
E
R
Y
O
N
E
M
Y
N
A
M
E
I
S
S
I
M
O
N
3, string and substring
One, String class
The String class itself is certainly a class because of the requirements of its naming convention, however, it is found that this class is special in use, and found that in the operation of the string class can still use "+" or "+ +" for string concatenation, you can also find that the data types of each data type as long as the string Class is uniformly converted to string, the use and characteristics of the string class are very important.
1,string comparison , the common functions involved are
Equals (), Equalsignorecase ()--------> Compare string contents
CompareTo (), comparetoignorecase ()----------> Compare string size
Regionmatches ()--------> matching substring
public class Stringtest {public static void main (string[] args) {String a = "ABC";
String B = "abc";
String c = "abcdef"; TODO auto-generated Method Stub System.out.println (a.equals (b));//string content comparison System.out.println A.equalsignorecase (b )//String content comparison, ignore case System.out.println (A.compareto (b));//Compare String size System.out.println (A.comparetoignorecase (b));// Compare string sizes, ignoring case/** * The following two methods are used to compare substrings of a specified range in two strings. In the entry parameters, Toffset and Ooffset respectively indicate the starting position of the substring in the current string and the starting address of the substring in the string to compare with; Len points out the comparison length. The previous method distinguishes between uppercase and lowercase letters, and if you write True at Boolean ignorecase, the * will be case-insensitive and write false to indicate that it will be case-sensitive. The latter method holds that there is a difference between uppercase and lowercase letters.
Thus, in fact, the previous method implied the function of the latter method. * Definition: * regionmatches (Boolean ignorecase,int toffset,string other,int ooffset,int len); regionmatches (int Toffset,string other,int ooffset,int len);/System.out.println (c.regionmatches (0, a, 0, 3));//case-sensitive System.out.pri Ntln (C.regionmatches (false,0, A, 0, 3));/Ibid. System.out.println (c.regionmatches (true,0,a, 0, 3));/case-insensitive}}
The corresponding output should be:
False true
-32
0
False
False
True
2,string Intercept, commonly used functions are
public string subString (int begin)----> Returns a new string starting from begin
The public string subString (int begin,int end)----> Returns a string that is end-begin from begin start length
Note: For example, there is a string abcdef, then a position is 0,b position is 1, and so on, and so on, F is 5, the string length is 6.
public class Stringtest {public
static void Main (string[] args) {
String a = "ABC";
String B = "abc";
String c = "abcdef";
System.out.println (a.substring (0));//Result ABC
System.out.println (a.substring (1))//Results BC
System.out.println (a.substring (2))//result C
System.out.println (c.substring (0, 4));/result ABCD
System.out.println (c.substring (1, 4));//Results BCD
}
}
The split () function is a commonly used function that removes a specific string and the remaining strings are returned as an array.
public class Stringtest {public
static void Main (string[] args) {
String c = ' abkefksdfksfwekwww ';
String []S1 = C.split ("K");
for (int i=0;i<s1.length;i++)
System.out.println (S1[i]);
}
The results are:
Ab
Ef
Sdf
Sfwe
Www