This time, I will review and study Java, which I have been running away from. As a result of touching Java in programming for the first time, I became conscious that I was not good at Java, but after learning Javascript and Ruby, what I learned began to be applied little by little in the Java field, and I became able to write Java little by little. Now, I want to learn Java well.
This time, we will review the methods of the String class that are originally provided in Java. The reference book I am using is the famous here.
1, equals A standard for string comparison. It will see if the strings to be compared are equal. It also looks at lowercase and uppercase letters.
sample.java
public class string {
public static void main(String[] args){
boolean result;
int length;
boolean empty;
//Check if the contents of the string are correct
String s1 = "this is Java";
String s2 = "This is Java";
String s3 = "This is java";
result = s1.equals(s2);
System.out.println("s1 : s2 => "+result);
result = s1.equals(s3);
System.out.println("s1 : s3 => "+result);
result = s2.equals(s3);
System.out.println("s2 : s3 => "+result);
result = "this is Java".equals(s1);
System.out.println("this is Java : s1 => "+result);
}
}
result.java
s1 : s2 => false
s1 : s3 => false
s2 : s3 => false
this is Java : s1 => true
2, equalsIgnoreCase It will judge whether the character string is the same regardless of uppercase or lowercase.
sample.java
result = s1.equalsIgnoreCase(s2);
System.out.println("s1 : s2 => "+result);
result = s1.equalsIgnoreCase(s3);
System.out.println("s1 : s3 => "+result);
result = s2.equalsIgnoreCase(s3);
System.out.println("s2 : s3 => "+result);
result = "this is Java".equalsIgnoreCase(s1);
System.out.println("this is Java : s1 => "+result);
result.java
s1 : s2 => true
s1 : s3 => false
s2 : s3 => false
this is Java : s1 => true
3, length It looks at the string length. Note that spaces are also viewed as a single letter.
sample.java
length = s1.length();
System.out.println("\"this is Java\"The string length of"+length+"is.");
length = s2.length();
System.out.println("\"This is Java\"The string length of"+length+"is.");
length = s3.length();
System.out.println("\"This is java\"The string length of"+length+"is.");
result.java
"this is Java"The string length of is 12.
"This is Java"The string length of is 12.
"This is java"The string length of is 9.
4, isEmpty Determine if the character string is empty. In the case of isEmpty, if there is a space, it will not be treated as empty. In the case of isBlank, it will be treated as empty even if it contains spaces.
sample.java
empty = s1.isEmpty();
System.out.println("s1.isEmpty : " + empty);
empty = s2.isEmpty();
System.out.println("s2.isEmpty : " + empty);
empty = s3.isEmpty();
System.out.println("s3.isEmpty : " + empty);
empty = "".isEmpty();
System.out.println("\"\".isEmpty : " + empty);
empty = " ".isEmpty();
System.out.println("\" \".isEmpty : " + empty);
empty = "".isBlank();
System.out.println("\"\".isBlank : " + empty);
empty = " ".isBlank();
System.out.println("\" \".isBlank : " + empty);
result.java
s1.isEmpty : false
s2.isEmpty : false
s3.isEmpty : false
"".isEmpty : true
" ".isEmpty : false
"".isBlank : true
" ".isBlank : true
Recommended Posts