I have a string say, "1.0+20*30.2-4.0/10.1" which I want to split in such a way that I will have a string array say
strArr = {"1.0", "20", "30.2", "4.0",10.1}
I wrote the following code for this
public class StringSplitDemo {
public static void main(String[] args) {
String str = "1.0+20*30.2-4.0/10.1";
String[] strArr = (str.split("[\\+-/\\*]"));
for(int i=0;i<strArr.length;i++)
System.out.print(strArr[i]+" ");
}
}
Rather than printing the expected output(by me) i.e 1.0 20 30.2 4.0 10.1 it prints
output: 1 0 20 30 2 4 0 10 1
which seems to split the string also around "." even if I didn't include it in the regex pattern.
What I'm missing here?
What is the right way to do this?
Use
String str = "1.0+20*30.2-4.0/10.1";
String[] strArr = str.split("[-+/*]");
System.out.print(Arrays.toString(strArr));
See the online Java demo
The [\\+-/\\*] character class matches more than just the 4 chars you defined, as - created a range between + and /.
You could fix the regex by escaping the hyphen, but the pattern looks much cleaner when you put the - at the start (or end) of the character class, where you do not have to escape it as there, the hyphen is treated as a literal -.
The issue was in regex
So you need to escape + otherwise it will treat it as atleast once
String[] strArr = (str.split("[\\-/\\*\\+]"));
By the way escape symbol here is not required. It can simply be written as
String[] strArr = (str.split("[-/*+]"));
Related
For my project I have to read various input graphs. Unfortunately, the input edges have not the same format. Some of them are comma-separated, others are tab-separated, etc. For example:
File 1:
123,45
67,89
...
File 2
123 45
67 89
...
Rather than handling each case separately, I would like to automatically detect the split characters. Currently I have developed the following solution:
String str = "123,45";
String splitChars = "";
for(int i=0; i < str.length(); i++) {
if(!Character.isDigit(str.charAt(i))) {
splitChars += str.charAt(i);
}
}
String[] endpoints = str.split(splitChars);
Basically I pick the first row and select all the non-numeric characters, then I use the generated substring as split characters. Is there a cleaner way to perform this?
Split requires a regexp, so your code would fail for many reasons: If the separator has meaning in regexp (say, +), it'll fail. If there is more than 1 non-digit character, your code will also fail. If you code contains more than exactly 2 numbers, it will also fail. Imagine it contains hello, world - then your splitChars string becomes " , " - and your split would do nothing (that would split the string "test , abc" into two, nothing else).
Why not make a regexp to fetch digits, and then find all sequences of digits, instead of focussing on the separators?
You're using regexps whether you want to or not, so let's make it official and use Pattern, while we are at it.
private static final Pattern ALL_DIGITS = Pattern.compile("\\d+");
// then in your split method..
Matcher m = ALL_DIGITS.matcher(str);
List<Integer> numbers = new ArrayList<Integer>();
// dont use arrays, generally. List is better.
while (m.find()) {
numbers.add(Integer.parseInt(m.group(0)));
}
//d+ is: Any number of digits.
m.find() finds the next match (so, the next block of digits), returning false if there aren't any more.
m.group(0) retrieves the entire matched string.
Split the string on \\D+ which means one or more non-digit characters.
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "123,45", "67,89", "125 89", "678 129" };
for (String s : arr) {
System.out.println(Arrays.toString(s.split("\\D+")));
}
}
}
Output:
[123, 45]
[67, 89]
[125, 89]
[678, 129]
Why not split with [^\d]+ (every group of nondigfit) :
for (String n : "123,456 789".split("[^\\d]+")) {
System.out.println(n);
}
Result:
123
456
789
String string = "3 5 3 -4 2 3 ";
I want to use split() here but I need to a separator to be -4. I don't know which numbers are negative and I need to use split to group positive numbers in separate arrays.
Is it possible?
Edit:
I want to use:
String[] parts = string.split(????);
and receive
parts[0] = "3 5 3"
parts[1] = "2 3"
From what I mentioned in comments, you can use -\\d+ for splitting.
It finds all the places where there is - followed by any number of digits. We can trim the array elements later if we want
Java Code
String Str = new String("3 5 3 -4 2 3");
String[] x = Str.split("-\\d+");
for (String retval: x){
System.out.println(retval.trim());
}
Ideone Demo
You can use the replace method to search for a given string (in this case, -4) and replace it with a delimiter of your choice, perhaps a pipe-bar line (|). Then you can use the split method and use the delimiter that's now inserted to split your array.
string = string.replace(replaceString, "|");
string[] parts = string.split('|');
Admittedly this is a little roundabout, but it's quick, easy, and will work.
Using StringUtils you can do something like this
public static void main(String[] args) {
String splitMe="123-457";
String [] newArray=StringUtils.split(splitMe, "-4");
for (String val:newArray){
System.out.println(val.toString());
}
}
output will be 123 for 1st index and 57 for second index
What is the way, how to split String with special character using Java?
I have very simple captcha like this:
5 + 10
String captcha = "5 + 10";
String[] captchaSplit = captcha.split("+");
And I get error:
Exception in thread "main" java.util.regex.PatternSyntaxException:
Dangling meta character '+' near index 0
How to fix it?
Thank you.
+ is a reserved character in regular expression and split takes regExp as a parameter. You can escape it by \\+ which will now match +.
Type it in square brackets
String captcha = "5 + 10";
String[] captchaSplit = captcha.split("[+]");
If you need to split String with multiple special symbols/characters, it's more convenient to use Guava library that contains Splitter class:
#Test
public void testSplitter() {
String str = "1***2***3";
List<String> list = Lists.newArrayList(Splitter.on("***").split(str));
Assert.assertThat(list.size(), is(3));
}
how to remove multiple token from string array in java by split along with [ ]
String Order_Menu_Name= [pohe-7, puri-3];
String [] s2=Order_Menu_Name.split("-|,");
int j = 0;
//out.println("s2.length "+s2.length);
while(j<s2.length){ }
and expected output should be each value separate.
e,g pohe 7 puri 3
Your question is not clear. Assuming that your string contains "pohe-7, puri-3" you can split them using a separator such as "," or "-" or whitespace. See below.
String Order_Menu_Name= "[pohe-7, puri-3]";
To remove "[" and "]" from the above String. you can use Java's replace method as follow:
Order_Menu_Name = Order_Menu_Name.replace("[", "");
Order_Menu_Name = Order_Menu_Name.replace("]", "");
You can replace the above two lines with one using regex expression that matches [....] if you wish to.
After you removed the above characters then you can split your string as follow.
String[] chunks = Order_Menu_Name.split(",");
i = 0;
while(chunks.length) {
System.out.println(chunks[i]);
i++;
}
You can pass one or two params to the Java split() method, one being the regex expression that defines the pattern to be found and the second argument is limit, specifying how many chunks to return, see below:
public String[] split(String regex, int limit)
or
public String[] split(String regex)
For example
String Str = new String("Welcome-to-Stackoverflow.com");
for (String retval: Str.split("-", 3)){
System.out.println(retval);
}
When splitting the above Str using seperator "-" you should get 3 chunks of strings as follow:
Welcome
to
Stackoverflow.com
If you pass the split function a limit of 2 instead of three then you get the following:
Welcome
to-Stackoverflow.com
Notice above "to-Stckoverflow.com" is returned as is because we limited the chunks to 2.
I'm using String.split() to divide some Strings as IPs but its returning an empty array, so I fixed my problem using String.substring(), but I'm wondering why is not working as intended, my code is:
// filtrarIPs("196.168.0.1 127.0.0.1 255.23.44.1 100.168.100.1 90.168.0.1","168");
public static String filtrarIPs(String ips, String filtro) {
String resultado = "";
String[] lista = ips.split(" ");
for (int c = 0; c < lista.length; c++) {
String[] ipCorta = lista[c].split("."); // Returns an empty array
if (ipCorta[1].compareTo(filtro) == 0) {
resultado += lista[c] + " ";
}
}
return resultado.trim();
}
It should return an String[] as {"196"."168"."0"."1"}....
split works with regular expressions. '.' in regular expression notation is a single character. To use split to split on an actual dot you must escape it like this: split("\\.").
Use
String[] ipCorta = lista[c].split("\\.");
in regular expressions the . matches almost any character.
If you want to match the dot you have to escape it \\..
Your statement
lista[c].split(".")
will split the first String "196.168.0.1" by any (.) character, because String.split takes a regular expression as argument.
However, the point, why you are getting an empty array is, that split will also remove all trailing empty Strings in the result.
For example, consider the following statement:
String[] tiles = "aaa".split("a");
This will split the String into three empty values like [ , , ]. Because of the fact, that the trailing empty values will be removed, the array will remain empty [].
If you have the following statement:
String[] tiles = "aaab".split("a");
it will split the String into three empty values and one filled value b like [ , , , "b"]
Since there are no trailing empty values, the result remains with these four values.
To get rid of the fact, that you don't want to split on every character, you have to escape the regular expression like this:
lista[c].split("\\.")
String.split() takes a regular expression as parameter, so you have to escape the period (which matches on anything). So use split("\\.") instead.
THis may help you:
public static void main(String[] args){
String ips = "196.168.0.1 127.0.0.1 255.23.44.1 100.168.100.1 90.168.0.1";
String[] lista = ips.split(" ");
for(String s: lista){
for(String s2: s.split("\\."))
System.out.println(s2);
}
}