Basics of character operation (java)

Character manipulation (java)

This is the first post. This time, I summarized the character operations. It's basic, so I'll look back when I forget it.

public class Main{
    public static void main(String[] args){
        String s1 = "Studying Java";
        String s2 = "Java";
        String s3 = "java";
        String s4 = "Javascript";
        String s5 = "javascript";
        String s6 = "";
        String s7 = "Study Java";
        String s8 = " j a v a ";
        String s9 = " j a v a ";
        String s10 = "jjjjjjjjjj";
        String s11 = "jjjjjaaaaavvvvvaaaaa";
        String s12 = "j";
        String s13 = "0123456789";
        String s14 = "a0b1c2d3e4f5g6h7i8j9k10";
        String s15 = "0a1b2c3d4e5f6g7h8i9j10k";
        String s16 = "a,0,b,1,c,2,d,3,e,4,f,5,g,6,h,7,i,8,j,9,k,10";

//Check if the character contents are equal
//The character you want to search.equals(The character you want to search)
if(s2.equals(s3)){
  System.out.println("s2 and s3 are the same character");
  }else{
        System.out.println("s2 and s3 are not equal");
  }

//Check if the characters are equal without case sensitivity The character you want to search for.equalsIgnoreCase(The character you want to search)
if(s2.equalsIgnoreCase(s3)){
 System.out.println("s2 and s3 are the same character");
 }else{
  System.out.println("s2 and s3 are not equal");
 }

//Display the length of the character string The character you want to search for.lenght()
System.out.println(s4.length());

//Check if it is an empty character The character you want to search.isEmpty()
if(s6.isEmpty()){
  System.out.println("s6 is an empty string");
 }else{
  System.out.println("s6 is not an empty string");
 }

//Find out if the string s is included The character you want to search for.contains(The character you want to search)
if(s4.contains(s2)){
 System.out.println("s2 is included in s4");
 }else{
 System.out.println("s4 does not include s2");
 }

//Find out if it starts with the string s The character you want to search for.startsWith(The character you want to search)
if(s4.startsWith(s2)){
  System.out.println("s4 starts with Java");
  }else{
  System.out.println("s4 doesn't start with Java");
  }

//Find out if it ends with the string s The character you want to search for.endsWith(The character you want to search)
  if(s4.endsWith(s2)){
  System.out.println("s4 ends with Java");
  }else{
   System.out.println("s4 does not end with Java");
  }

//Find the place where the character string s first appears from the beginning. The character you want to search.indexof(Search character)
System.out.println(s2.indexOf(s4)); //-If 1, the search character does not exist
System.out.println(s4.indexOf(s2)); //0 or more is the position where the search character occurs

//Find the place where the string s first appears from behind. The character you want to search.lastIndexOf(Search character)
System.out.println(s2.lastIndexOf(s4)); //-If 1, the search character does not exist
System.out.println(s7.lastIndexOf(s2)); //0 or more is the position where the search character occurs

//Cut out only one character at the specified position The character you want to search.charAt(Location of the character you want to cut out)
System.out.println(s7.charAt(1)); //Strong is displayed

//Cut out characters of arbitrary length from the specified position Characters you want to search.substring(Location of the character you want to cut out)
System.out.println(s7.substring(1)); //Strong Java displays
System.out.println(s7.substring(1,4)); //Strong Ja is displayed

//Convert uppercase to lowercase Characters you want to convert.toLowerCase()
System.out.println(s2.toLowerCase()); //Java becomes java

//Convert lowercase letters to uppercase characters.toUpperCase()
System.out.println(s2.toUpperCase()); //java becomes JAVA

//Eliminate whitespace before and after characters Characters you want to convert.trim()
System.out.println(s8.trim()); //j a v a becomes JAVA

//Replace string Characters you want to convert.replace(Characters you want to convert,Converted characters)
System.out.println(s9.replace("j","a")); //Displayed as a a v a
System.out.println(s9.replace(" ","")); //Remove whitespace in java and display characters

StringBuilder hoge = new StringBuilder(); //New StringBuilder instance to hoge
  for(int i = 0; i < 1000; i++) { //Repeat for internal processing 1000 times
  hoge.append("a"); //Add a to hoge
  hoge.append("z"); //Add z to hoge
 }

//Character pattern check(Regular expressions)The character you want to search.matches(Characters to compare)
//Do the characters in the variable match the characters in Java?
System.out.println(s7.matches("Study Java")); //true
System.out.println(s7.matches("study")); //false
System.out.println(s7.matches("Java")); //false

Recommended Posts

Basics of character operation (java)
Summary of Java language basics
Java basics
Java basics
Java basics
Memorandum of new graduate SES [Java basics]
java programming basics
[# 1 Java] Basics of Java-Major premise before studying-
Basics of Ruby
Java JAR basics
[day: 5] I summarized the basics of Java
Object-oriented (Java) basics
Looking back on the basics of Java
Java concurrency basics
[Java] Comparison of String type character strings
Java character code
[Java] Overview of Java
Basics of java basics ② ~ if statement and switch statement ~
[java] Summary of how to handle character strings
I understood the very basics of character input
Basics of threads and Callable in Java [Beginner]
[Introduction to Java] Basics of java arithmetic (for beginners)
[Java] File system operation
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
java --Unification of comments
Java programming basics practice-array
History of Java annotation
java (merits of polymorphism)
Java Network Basics (Communication)
Basics of try-with-resources statement
Java8 Stream reduction operation
NIO review of java
Muscle Java Basics Day 1
[Java] Three features of Java
Summary of Java support 2018
[For beginners] Quickly understand the basics of Java 8 Lambda
[In-house study session] Basics of Java annotation (2017/11/02) ~ Under construction ~
I summarized the types and basics of Java exceptions
About full-width ⇔ half-width conversion of character strings in Java
[Java] Object operation of ArrayList class (AOJ ④ Inversion of sequence)
[Java] Handling of character strings (String class and StringBuilder class)
About an instance of java
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
Docker monitoring-explaining the basics of basics-
[Java] Practice of exception handling [Exception]
[GCD] Basics of DispatchQueue class
[Java11] Stream Summary -Advantages of Stream-
I tried to summarize the basics of kotlin and java
[Java] Comparison method of character strings and comparison method using regular expressions
About simple operation of Docker
[Java] Creation of original annotation
Understand the basics of docker
4th day of java learning
[Java] Beginner's understanding of Servlet-①
Java end of month plusMonths
[Java] Summary of regular expressions