First recognized java String and StringBuffer (on), stringstringbuffer
I haven't written a blog for a long time. I 've been struggling with how to develop my post? Okay, no nonsense !! Start !!
The String and StringBuffer classes are the most commonly used classes in development. Let's take a look at these two classes. First, let's talk about the String method:
String s = "123456 ";
1. length () method:
This method is used to obtain the string length. It is often used to log on to the registration page to determine whether the character length entered by the user is valid;
Example:
String pswd = "abc1234567 ";
If (pawd. length ()> 6 ){
System. out. println ("Login successful! ");
} Else {
System. out. printin ("Logon Failed ");
}
2. equals () method
Java comes with two string comparison methods. The first is equals, which is a strict type comparison, which distinguishes the size. The second is sisnorecase (), it is used to judge the case-insensitive comparison of strings.
They are all strings of the comparison, and "=" is to judge the address of the string in the memory, there is a difference between the two;
Example:
Equals method example:
String B = "abc123 ";
String c = "ABC123 ";
If (B. equals (c )){
System. out. println ("Equal ");
} Else {
System. out. println ("not equal ");
}
Example of the equalsIsnoreCase method:
String B = "abc123 ";
String c = "ABC123 ";
If (B. equalsIgnoreCase (c )){
System. out. println ("Equal ");
} Else {
System. out. println ("not equal ");
}
"=" Example:
String B = "abc123 ";
String c = "abc123 ";
If (B = c ){
System. out. println ("Equal ");
} Else {
System. out. println ("not equal ");
}
3. Use the toLowerCase () and toUpperCase () Methods
ToLowerCase () converts a string to lowercase
ToUpperCase () is to convert the string into uppercase
Example:
String B = "abc123 ";
String c = "abc123 ";
B. toLowerCase ();
C. toUpperCase ();
System. out. println (B );
System. out. println (c );
4. split () method
The function is to split a string into multiple strings.
Example:
String c = "abc-123 ";
String [] d = c. split ("-");
System. out. println (d [0]);
System. out. println (d [1]);
5. indexOf () method
It is used to find the position where the character you are searching for is in the string, starting from zero subscript.
String B = "abc123 ";
String c = "abc-123 ";
Int d = c. indexOf ("2 ");
System. out. println (d );
I will write it here today. Here are a few interesting questions. You can reply in a comment:
1. What is the difference between equals and "=?
2. There is a landline number: 0731-76333665. I need to know whether its zone number and landline number (four area numbers and seven landline numbers) comply with the specifications. What should I do?
We recommend a string blog: http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html;