This question already has answers here:
The split() method in Java does not work on a dot (.) [duplicate]
(7 answers)
Closed 9 years ago.
My problem is that the array ms[ ] doesn't get values when I do split( );
Why is this happening ?
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss."); //change format
String msgTime = ft.format(date);
System.out.println(msgTime);
String ms[] = msgTime.split(".");
System.out.println(ms.length);
}
}
The problem is split() function takes regular expression as argument, not a simple string. And "." regular expression means "any symbol".
So you need just escape it.
String ms[] = msgTime.split("\\.");
I'm guessing you meant to do
String ms[] = msgTime.split("\\.");
String.split() takes a regular expression so you should escape any special characters, such as ..
Related
This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 3 years ago.
In my program I want to remove all the dots(.) I've tried to do this but it's not working.
public class MyClass {
public static void main(String args[]) {
String str = " .Hello.World..Awesome!. ";
System.out.println(str.replaceAll(".",""));
}
}
Replace all takes a regex as the first argument and you need to escape "." like
"\\."
str.replaceAll("\\.","")
This is not working because . is a special regex character. You must escape this using a backslash. The dot is used as a capture all in regex.
System.out.println(str.replaceAll("\\.",""));
You can read up on all of the special characters in regex here - https://www.regular-expressions.info/characters.html
This question already has answers here:
Regex to match only commas not in parentheses?
(3 answers)
Closed 3 years ago.
I've the string looking like this:
word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?|word1-word2-\d{1,2}\w?-\d{1,2}\w?|word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w
I'd like to split this string by '|' everywhere where it doesn't precedes by '('. So result should be:
["word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w"]
I've trying to use negative lookahead \((?!\|) which split the text to on '('.
UPDATE
So I want to achieve not splitting the "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w" on '|' where that character is precedes by '('.
Could someone please help me with this?
Code:
public static void main(String[] args) {
String str = "word1-word2-word3-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-word3-(\\d{1,2}\\w?\\d{1,2}|\\d{1,2}\\w?)-\\d{1,2}\\w";
String[] arrOfStr = str.split("\\|");
for (String a : arrOfStr)
System.out.println(a);
}
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I was expecting the OUTPUT to be 3 from the below class:
public class Pattern {
public static void main(String[] args) {
String data = "111,577,5099,541,142,
2015-08-01 00:08:42,2015-08-01 06:31:52|
674,898,7061,36,105,2015-08-01 19:28:45,
2015-08-02 14:46:27|948,522,1840,66,889,
2015-08-02 13:04:56,2015-08-02 19:39:57";
if(data.contains("|"))
{
String pattern[] = data.split("|");
System.out.println("the pattern length: "+pattern.length);
}
}
}
OUTPUT:
the pattern length: 180
The character | is a special character in regexes (the split method uses regexes)
You'll have to go with
String pattern[] = data.split("\\|");
kindly update your code likewise,
String pattern[] = data.split("\\|");
This question already has an answer here:
using parenthesis replaceAll method Java
(1 answer)
Closed 7 years ago.
In the code below I'm trying to replace in text occurrences of fromString withtoString, but no replacement takes place. How to set the parenthesis in the regex to make this work?
public static void main(String[] args) {
String fromString = "aaa(bbb)";
String toString = "X";
String text = "aaa(bbb) aaa(bbb)";
String resultString = text.replaceAll(fromString, toString);
System.out.println(resultString);
}
replaceAll uses regex as its first argument. Parenthesis () are used for capturing groups so need to be escaped
String fromString = "aaa\\(bbb\\)";
Since you can't modify the input String you can use Pattern.quote
String resultString = text.replaceAll(Pattern.quote(fromString), toString);
or simply String.replace could be used which doesnt use regex arguments
String resultString = text.replace(fromString, toString);
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
This is probably a very easy question but I'm going to give you the code first.
import java.util.Scanner;
public class help {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Give: ");
String s = sc.next();
String[] parts = s.split(".");
System.out.println(parts.length);
}
}
Even if I give 192.168.1.1.1.1.1 or 1.2.3 or ... the parts.length will always be 0, can somebody please explain to me why and how I can let it be 4 if i enter 1.2.3.4?
You need s.split("\\.") because the argument to split is a regular expression. The . character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".
Because "." is a special character, meaning "any character".
You need to escape it to be able to use it as the character ".":
String[] parts = s.split("\\.");