[Note] Java: String survey

Introduction

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.

String operation

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

[Note] Java: String survey
[Note] Java: String search
Java string
[Java] String padding
Java string processing
Split string (Java)
Java abstract modifier [Note]
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: Measures the speed of string concatenation
Java JUnit brief note
[java] Java SE 8 Silver Note
About Java String class
My Study Note (Java)
Java 8 Completable Future Survey
java: Add date [Note]
A note about Java GC
Java inflexible String class substring
(Note) Java classes / variables / methods
Reflection on Java string manipulation
[Java] About String and StringBuilder
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Study Java with Progate Note 1
[Java] Speed comparison of string concatenation
Note
[Note] Java Silver SE8 qualification acquired
Note
Java
[Note] Handling of Java decimal point
My note: Introducing Java to Ubuntu
String
Studying Java 8 (String Joiner and join)
Java
[Java] Correct comparison of String type
Note: Differences from Java as seen from C #
[Java] Divide a character string by a specified character
Java date data type conversion (Date, Calendar, String)
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
Pass parameters when debugging vscode java. [Note]
[Beginner] Java method / class / external library [Note 23]
[Java] Data type / string class cheat sheet
Notes on operators using Java ~ String type ~
[Java] Convert Object type null to String type
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
How to output Java string to console screen
Basic knowledge of Java development Note writing
Note: next ・ nextLine (paiza learning Java introduction 9: # 06)
[Note] Cooperation between Java and DB (basic)
[Java] Character judgment / character string formatting (AOJ11 --character count)
All same hash code string in Java
How to use Java Scanner class (Note)
Split a string with ". (Dot)" in Java
[Java] Comparison of String type character strings
Survey of Java 8 Function, Consumer, Supplier, Predicate
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Cheking Prime number --Java string program examples
Type conversion from java BigDecimal type to String type