I have a list of titles that I want to save as a String:
- title1
- title2
- title|3
Now, I want to save this as a single line String delimited by |, which would mean it ends up like this: title1|title2|title|3.
But now, when I split the String:
String input = "title1|title2|title|3";
String[] splittedInput = input.split("\\|");
splittedInput will be the following array: {"title1", "title2", "title", "3"}.
Obviously, this is not what I want, I want the third entry of the array to be title|3.
Now my question: how do I correctly escape the | in the titles so that when I split the String I end up with the correct array of three titles, instead of 4?
#Gábor Bakos
Running this code snippet:
String input = "title1|title2|title\\|3";
String[] split = input.split("(?<!\\\\)\\|");
for (int i = 0; i < split.length; i++) {
split[i] = split[i].replace("\\\\(?=\\|)", "");
}
System.out.println(Arrays.toString(split));
I get this output: [title1, title2, title\|3]. What am I doing wrong?
You can use anything. For example with \:
"title1|title2|title\\|3".split("(?<!\\\\)\\|").map(_.replaceAll("\\\\(?=\\|)", "")) //Scala syntax
Resulting:
Array(title1, title2, title|3)
The final mapping is required to remove the escaping character too.
(?<!\\\\) is look behind, (?=\\|) is an extra look-ahead for the escaped |.
Well if you use a TSV format the chosen separator must never be left unescaped in the data.
You could simply escape your data (for ex, title1|title2|title\|3) and you would then split on (?<!\\)| (negative lookbehind).
In Java, it gives:
public static void main(String[] args) {
// prints out [title1, title2, title|3, title|4]
System.out.println(parsePipeSeparated("title1|title2|title\\|3|title\\|4"));
}
private static List<String> parsePipeSeparated(String input) {
return Stream.of(input.split("(?<!\\\\)\\|"))
.map(escapedText -> escapedText.replace("\\|", "|"))
.collect(Collectors.toList());
}
Use another separator, for instance "title1,title2,title|3", instead of "title1|title2|title|3". And then split(",")
Related
3 columns from Pipe delimited 7columns using regex in java
Example:
String:
10|name|city||date|0|9013
i only want upto city(3 columns):
Expected output:
10|name|city
means: i want number of columns based on | using regex.
Thank you.
I would use a simple regex pattern with the split method. There's probably a more elegant way to handle the pipes in the resulting string but this should give you an idea, goodluck!
public static void main(String[] args) {
String str = "10|name|city||date|0|9013";
// split the string whenever we see a pipe
String[] arrOfStr = str.split("\\|");
StringBuilder sb = new StringBuilder();
// loop through the array we generated and format our output
// we only want the first three elements so loop accordingly
for (int i = 0; i < 3; i++) {
sb.append(arrOfStr[i]+"|");
}
// remove the trailing pipe
sb.setLength(sb.length() - 1);
System.out.println(sb.toString());
}
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 have a need to split a string that is passed in to my app from an external source. This String is delimited with a caret "^" and here is how I split the String into an Array
String[] barcodeFields = contents.split("\\^+");
This works fine except that some of the passed in fields are empty and I need to account for them. I need to insert either "" or "null" or "empty" into any missing field.
And the missing fields have consecutive delimiters. How do I split a Java String into an array and insert a string such as "empty" as placeholders where there are consecutive delimiters?
The answer by mureinik is quite close, but wrong in an important edge case: when the trailing delimiters are in the end. To account for that you have to use:
contents.split("\\^", -1)
E.g. look at the following code:
final String line = "alpha ^beta ^^^";
List<String> fieldsA = Arrays.asList(line.split("\\^"));
List<String> fieldsB = Arrays.asList(line.split("\\^", -1));
System.out.printf("# of fieldsA is: %d\n", fieldsA.size());
System.out.printf("# of fieldsB is: %d\n", fieldsB.size());
The above prints:
# of fieldsA is: 2
# of fieldsB is: 5
String.split leaves an empty string ("") where it encounters consecutive delimiters, as long as you use the right regex. If you want to replace it with "empty", you'd have to do so yourself:
String[] split = barcodeFields.split("\\^");
for (int i = 0; i < split.length; ++i) {
if (split[i].length() == 0) {
split[i] = "empty";
}
}
Using ^+ means one (or more consecutive) carat characters. Remove the plus
String[] barcodeFields = contents.split("\\^");
and it won't eat empty fields. You'll get (your requested) "" for empty fields.
The following results in [blah, , bladiblah, moarblah]:
String test = "blah^^bladiblah^moarblah";
System.out.println(Arrays.toString(test.split("\\^")));
Where the ^^ are replaced by a "", the empty String
I want to separate a string with special characters for example:
String s = ",?hello=glu()Stop/<><$#!gluglufazoperu";
I use the split function to obtain the normal characters:
hello
glu
Stop
gluglufazoperu
I have a problem, when I use the split it puts whitespace in the begining of the string array, anyone knows how to remove it?
Here is my code example:
public class Main {
public static void main(String[] args) {
String s = ",?hello=glu()Stop/<><$#!gluglufazoperu";
String f[] = s.split("[^\\w \\s]+");
int i= 0;
while(i < f.length){
System.out.println(f[i]);
i++;
}
}
}
This is the output:
(whitespace)
hello
glu
Stop
gluglufazoperu
there is a whitespace because the first split between ',' and '?' returns an empty string "".
With your while loop you print System.out.println(""), and that is an empty line.
When you only want to print the not empty strings you should replace your System.out.println with
if(!"".equals(f[i])){
System.out.println(f[i]);
}
And (beside your question) a little tip, take a look at this tutorial.
oracle for loop tutorial
Not whitespace but empty string as ",?" is a separator too. This could happen at the end too.
You might simple skip an empty string.
You might remove those from the array, which is costly, as it makes a copy.
if (f.length > 1) {
if (f[0].isEnpty()) {
f = Arrays.copyOfRange(f, 1, f.length);
}
if (f.length > 1) {
if (f[f.length - 1].isEnpty()) {
f = Arrays.copyOfRange(f, 0, f.length - 1);
}
}
}
Remarks:
String[] f = ... is more conventional in java.
\\s already captures the space, and do you really want to keep spaces?
You could simply replace the longest prefix the string matching your regular expression with the empty string before splitting:
final String regex = "[^\\w \\s]+";
String f[] = s.replaceAll("^"+regex, "")
.split(regex);
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);
}
}