Reflection on Java string manipulation

Recently, I implemented a process to play with character strings in my work.

Sample.java


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Split {
	public static void main(String[] args) {
       //Theme: Divide a string of length 10 into 5 blocks, 2 characters each
	        String sampleString = "0123456789";
	        List<String> splitedStringList = new ArrayList<String>();
	        Matcher m = Pattern.compile("[\\s\\S]{1,2}").matcher(sampleString);
	        while (m.find()) {
	        	splitedStringList.add(m.group());
	        }
	         
	        for (String element : splitedStringList) {
	        	System.out.println(element); //01 23 45 76 89 is displayed with line breaks
	       }
	}
}

If you make full use of what it is a regular expression, you can write a process like ↑ that "it seems to do something cool at first glance".

Or rather, I made that kind of implementation at first.

This is because split () is based on the delimiter specified in the argument, so you cannot specify "X characters at a time".

However, from the on-site leader who passed behind

"Should I use substring ()?"

After all, it's enough because it only separates strings with super-simple rules. However, the idea of using it during implementation did not come up at all. It's embarrassing.

Even with one implementation, there are many things to consider, such as processing performance and code readability ...

・ If it can be realized by simple processing, follow it.

・ Increase the number of drawers in your head and allow them to be pulled out freely.

Not limited to Java, it was a case that I should personally reflect on.

Recommended Posts

Reflection on Java string manipulation
Java string
[Java] String padding
Notes on operators using Java ~ String type ~
Java string processing
Split string (Java)
Let's touch on Java
String manipulation Excel function
Install Java on Mac
[Java] String comparison and && and ||
Run PostgreSQL on Java
Java string multiple replacement
Java basic date manipulation
[Note] Java: String search
[Note] Java: String survey
About Java String class
Date manipulation in Java 8
Java version control on macOS
Install OpenJDK7 (JAVA) on ubuntu 14.04
Downgrade Java on openSUSE Linux
Java inflexible String class substring
On passing Java Gold SE 8
Oracle Java 8 on Docker Ubuntu
Install Java on WSL Ubuntu 18.04
[Java] About String and StringBuilder
Run java applet on ubuntu
Put Oracle Java 8 on CircleCI 2
Java version change on CentOS
Install java 1.8.0 on Amazon linux2
[Java] Speed comparison of string concatenation
Run Java EE applications on CICS
Using Java on OSX 10.15 (Catalina) β
Consideration on Java Persistence Framework 2017 (Summary) -1
Various methods of Java String class
Consideration on Java Persistence Framework 2017 (6) Ebean
Personal memo: Metaprogramming with Java reflection
Install Java with zip on Windows
Get Java Silver on your commute!
Consideration on Java Persistence Framework 2017 (5) Iciql
[Java] How to update Java on Windows
Consideration on Java Persistence Framework 2017 (7) EclipseLink
Install Java Open JDK 8 on CentOS 7
Is Java on AWS Lambda slow?
Hello World on AWS Lambda + Java
Arbitrary string creation code by Java
Notes on signal control in Java
Consideration on the 2017 Java Persistence Framework (1)
Consideration on Java Persistence Framework 2017 (8) Hibernate5
Java9 (based on Oracle JVM) catchup
Run tomcat shell script on java8
Enable Java 8 and Java 11 SDKs on Ubuntu
Java cannot be installed on Ubuntu 13.04
Notes on Android (java) thread processing
Consideration on Java Persistence Framework 2017 (2) Doma2
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
Notes on Java path and Package
Install Java 9 on windows 10 and CentOS 7
Develop gRPC on Windows + Java 7 + Maven 2
Install Java development environment on Mac
[Java] Display the bit string stored in the byte type variable on the console