[Java] Comparison method of character strings and comparison method using regular expressions

Introduction

When comparing strings, you cannot simply compare with (int1 == int2) like the int type. When comparing strings, use one of the string methods, ** equals () **, instead of the comparison operator.

String comparison --equals ()

** equals () ** is extremely easy to use, just put the string you want to compare in the argument. For example, use it as follows.

if(String1.equals(String2)){
   //The instruction you want to execute
}

When deciding the condition of the if statement, it is OK if you put String1.equals (String2) in ().

bonus

After learning the equals () method, I will introduce regular expressions, their comparison methods, and how to extract strings.

What is a regular expression?

A regular expression is a writing style used when you want to express a patterned character string. For example, it is used to express a zip code or credit card number.

For details on how to use it, see Regular expression basics for easy understanding.

Regular expression comparison-matches ()

If you want to compare variables using regular expressions, use matches ().

if(String1.matches(String2)){
   //The instruction you want to execute
}

Usage is not much different from ʻequals () `.

Extract string --substring ()

If you want to retrieve a specific string, use substring (). The first argument is the start index, which is the beginning of the string, and the second argument is the end index, which is the end of the string, plus a number.

String str = "AppleLemonApple";
String strget = str.substring(5,10); // Lemon

In this example, we are extracting the string Lemon. In Java, the index starts at 0, so L is the 5th.

Recommended Posts

[Java] Comparison method of character strings and comparison method using regular expressions
[Java] Comparison of String type character strings
[Java] Summary of regular expressions
[Java] Handling of character strings (String class and StringBuilder class)
Java comparison using the compareTo () method
Implementation of validation using regular expressions
Name a group of regular expressions (Java)
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Collection and StringBuilder operation method comparison
Comparison of thread implementation methods in Java and lambda expression description method
Match IP addresses using regular expressions in Java
[Java] String comparison and && and ||
colorize and regular expressions
The comparison of enums is ==, and equals is good [Java]
[Java] Sort the list using streams and lambda expressions
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
[RSpec] WebMock handles regular expressions and Array query strings
Equivalence comparison of Java wrapper classes and primitive types
About full-width ⇔ half-width conversion of character strings in Java
[Java10] Be careful of using var and generics together
[Java] Cut out a part of the character string with Matcher and regular expression
Setting method to link Java of Eclipse and Github / September 2017
Basics of character operation (java)
Java methods and method overloads
Advantages and disadvantages of Java
Benefits of Java static method
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
Handling of line beginning and line ending in regular expressions in Ruby
Comparison of WEB application development with Rails and Java Servlet + JSP
Comparison of processing times based on awk, shell command, and Java
[Java] Speed comparison of string concatenation
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
[Java] Remove whitespace from character strings
Java for beginners, expressions and operators 2
[Java beginner] == operator and equals method
Handling of time zones using Java
[Java] Judgment of identity and equivalence
[Algorithm] Descending order of character strings
Try using IBM Java method tracing
[Java] Correct comparison of String type
Summary of object-oriented programming using Java
The origin of Java lambda expressions
I tried using GoogleHttpClient of Java
Deploy an image consisting of Tomcat, Java and MySQL using Ansible and Jenkins
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
Display Japanese calendar and days of the week using java8 standard class
Implementation of clone method for Java Record
Convert Java org.w3c.dom.Document objects and XML strings
Comparison of Android Handler Looper and runOnUiThread
Java Silver exam procedure and learning method
After 3 months of Java and Spring training
Nowadays Java lambda expressions and Stream API
[Java] Inheritance and structure of HttpServlet class
Thorough comparison of Android and iOS implementations
Implementation of tabs using TabLayout and ViewPager
Java and Derby integration using JDBC (using NetBeans)
Status monitoring of java application using Elasticsearch
[Java] Precautions when comparing character strings with character strings
Acquisition of input contents using Scanner (Java)