Split a string with ". (Dot)" in Java

I've been writing Java for the first time in a while, and when I tried to separate strings with ., I was lightly addicted to it.

Python looks like this.

hoge = '172.0.0.1'
fuga = hoge.split('.') # => ['172', '0', '0', '1']

In Java with the same feeling

String hoge = "172.0.0.1";
String[] fuga = hoge.split(".");

If you write, an array of length 0 will be returned.

The cause is that Java split takes a pattern string as an argument. . Matches any single character. (..) φ memo memo

String hoge = "172.0.0.1";
String[] fuga = hoge.split("7."); // => ["1", ".0.0.1"]

If you expect the same behavior as Python:

import java.util.regex.Pattern;

String hoge = "172.0.0.1";
String[] fuga = hoge.split(Pattern.quote("."));

Recommended Posts

Split a string with ". (Dot)" in Java
Read a string in a PDF file with Java
Split string (Java)
Create a CSR with extended information in Java
Code to escape a JSON string in Java
Find a subset in Java
Quickly implement a singleton with an enum in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
Implementing a large-scale GraphQL server in Java with Netflix DGS
Convert a Java byte array to a string in hexadecimal notation
How to store a string from ArrayList to String in Java (Personal)
Create a SlackBot with AWS lambda & API Gateway in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Build a Java project with Gradle
Morphological analysis in Java with Kuromoji
3 Implement a simple interpreter in Java
A simple sample callback in Java
Get stuck in a Java primer
Play with Markdown in Java flexmark-java
I can't create a Java class with a specific name in IntelliJ
About returning a reference in a Java Getter
[Java] Divide a character string by a specified character
What is a class in Java language (3 /?)
When seeking multiple in a Java array
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
Concurrency Method in Java with basic example
[Creating] A memorandum about coding in Java
Java creates a table in a Word document
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Extract a part of a string with Ruby
Try making a calculator app in Java
Notation to put a variable in a string
Read xlsx file in Java with Selenium
All same hash code string in Java
Implement something like a stack in Java
Creating a matrix class in Java Part 1
Working with huge JSON in Java Lambda
Configure a multi-project with subdirectories in Gradle
[Personal memo] How to interact with a random number generator in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I wrote a Lambda function in Java and deployed it with SAM
I want to ForEach an array with a Lambda expression in Java
<java> Split the address before and after the street address with a regular expression
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
The story of low-level string comparison in Java
[Java] Difference between equals and == in a character string that is a reference type
Two ways to start a thread in Java + @
Let's make a calculator application with Java ~ Create a display area in the window
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
A story about the JDK in the Java 11 era
Partization in Java
Static code analysis with Checkstyle in Java + Gradle
How to display a web page in Java
[Android / Java] Operate a local database in Room
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Convert a string to a character-by-character array with swift