Remove or Replace "|" character by String method - java

I would like to remove character "|" by using String#replaceAll() method.
But first parameter is recognized regex meta character.
I tried replaceAll("\|", ""); with escape character, but it cannot be compiled.
Are there any way to remove or replace "|" character by Java?

You need to double-escape the | when using replaceAll(), like:
myString.replaceAll("\\|", "");
This is because your string actually gets parsed twice, first as a literal string and then as a regular expression. So when you start with "\\|" the first parse gives you a literal string of \|, which the regex parser then recognizes as |. This can be a bit confusing until you get used to it.

The correct answer is, don't use replaceAll() (which replaces regexes), use replace() which simply replaces a character:
replace("|", "");
fyi, despite the method name not having "all" in it, is does in fact replace all instances of the specified character.

Also, this does the job too:
String test = "Hello | Hi | Test";
System.out.println(test.replace("|", ""));
String replace method is a useful tool here. No need to use regexes.

Related

Java - Splitting String [duplicate]

I am wondering if I am going about splitting a string on a . the right way? My code is:
String[] fn = filename.split(".");
return fn[0];
I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.
split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :
String[] fn = filename.split("\\.");
return fn[0];
I see only solutions here but no full explanation of the problem so I decided to post this answer
Problem
You need to know few things about text.split(delim). split method:
accepts as argument regular expression (regex) which describes delimiter on which we want to split,
if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.
You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).
So for string like "abc" if we split on "." split method will
create array like ["" "" "" ""],
but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.
Solution
To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).
So solution to your problem may look like
String[] fn = filename.split("\\.");
Bonus
You can also use other ways to escape that dot like
using character class split("[.]")
wrapping it in quote split("\\Q.\\E")
using proper Pattern instance with Pattern.LITERAL flag
or simply use split(Pattern.quote(".")) and let regex do escaping for you.
Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:
String[] fn = filename.split("\\.");
(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)
Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:
int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);
the String#split(String) method uses regular expressions.
In regular expressions, the "." character means "any character".
You can avoid this behavior by either escaping the "."
filename.split("\\.");
or telling the split method to split at at a character class:
filename.split("[.]");
Character classes are collections of characters. You could write
filename.split("[-.;ld7]");
and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").
As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -
String[] fn = filename.split("\\.");
return fn[0];
In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !
String str="1.2.3";
String[] cats = str.split(Pattern.quote("."));
Wouldn't it be more efficient to use
filename.substring(0, filename.indexOf("."))
if you only want what's up to the first dot?
Usually its NOT a good idea to unmask it by hand. There is a method in the Pattern class for this task:
java.util.regex
static String quote(String s)
The split must be taking regex as a an argument... Simply change "." to "\\."
The solution that worked for me is the following
String[] fn = filename.split("[.]");
Note: Further care should be taken with this snippet, even after the dot is escaped!
If filename is just the string ".", then fn will still end up to be of 0 length and fn[0] will still throw an exception!
This is, because if the pattern matches at least once, then split will discard all trailing empty strings (thus also the one before the dot!) from the array, leaving an empty array to be returned.
Using ApacheCommons it's simplest:
File file = ...
FilenameUtils.getBaseName(file.getName());
Note, it also extracts a filename from full path.
split takes a regex as argument. So you should pass "\." instead of "." because "." is a metacharacter in regex.

About string split method in java [duplicate]

I am wondering if I am going about splitting a string on a . the right way? My code is:
String[] fn = filename.split(".");
return fn[0];
I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.
split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :
String[] fn = filename.split("\\.");
return fn[0];
I see only solutions here but no full explanation of the problem so I decided to post this answer
Problem
You need to know few things about text.split(delim). split method:
accepts as argument regular expression (regex) which describes delimiter on which we want to split,
if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.
You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).
So for string like "abc" if we split on "." split method will
create array like ["" "" "" ""],
but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.
Solution
To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).
So solution to your problem may look like
String[] fn = filename.split("\\.");
Bonus
You can also use other ways to escape that dot like
using character class split("[.]")
wrapping it in quote split("\\Q.\\E")
using proper Pattern instance with Pattern.LITERAL flag
or simply use split(Pattern.quote(".")) and let regex do escaping for you.
Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:
String[] fn = filename.split("\\.");
(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)
Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:
int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);
the String#split(String) method uses regular expressions.
In regular expressions, the "." character means "any character".
You can avoid this behavior by either escaping the "."
filename.split("\\.");
or telling the split method to split at at a character class:
filename.split("[.]");
Character classes are collections of characters. You could write
filename.split("[-.;ld7]");
and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").
As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -
String[] fn = filename.split("\\.");
return fn[0];
In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !
String str="1.2.3";
String[] cats = str.split(Pattern.quote("."));
Wouldn't it be more efficient to use
filename.substring(0, filename.indexOf("."))
if you only want what's up to the first dot?
Usually its NOT a good idea to unmask it by hand. There is a method in the Pattern class for this task:
java.util.regex
static String quote(String s)
The split must be taking regex as a an argument... Simply change "." to "\\."
The solution that worked for me is the following
String[] fn = filename.split("[.]");
Note: Further care should be taken with this snippet, even after the dot is escaped!
If filename is just the string ".", then fn will still end up to be of 0 length and fn[0] will still throw an exception!
This is, because if the pattern matches at least once, then split will discard all trailing empty strings (thus also the one before the dot!) from the array, leaving an empty array to be returned.
Using ApacheCommons it's simplest:
File file = ...
FilenameUtils.getBaseName(file.getName());
Note, it also extracts a filename from full path.
split takes a regex as argument. So you should pass "\." instead of "." because "." is a metacharacter in regex.

remove slash from jsonstring jackson lib java

I have large json string something like this:
[{\"name\":\"Nick\",\"role\":\"admin\",\"age\":\"32\",\"rating\":47}]
I want to remove every occurrence of \" with " in string.
for this i used String's `relaceAll("\\"","\"")
when i am print the string after replace its printing fine but when i am sending string to object in json. its appending slash , please guide me how to get rid of this slash
My expecting result:
[{"name":"Nick","role":"admin","age":"32","rating":47}]
For this i used String's relaceAll("\\"","\"") ...
The String#replaceAll() method interprets the argument as a RegEx (Regular Expression). The Backslash Character (\) is an escape character in both String & Regex.
Hence, you need to double-escape it for the RegEx to work.
Example:
myString = myString.replaceAll("\\\\", "\\\\\\\\");
You can also use String#replace() method to perfrom the same task like this:
myString = myString.replace("\\", "\\\\");

String replaceAll not working for $

I am trying to strip out certain "<" and ">" from HTML code that is being generated by a 3rd party (of morons)
I am doing a replaceAll for some certain left over conditions that are not being picked up by our ETL people.
I have this string: "<$200" and I need it to be XML compliant like "<$200"
string.replaceAll("<$200","<$200");
does not work. I assume it is some regEx funkyness. What is the correct way to do this?
String#replaceAll accepts a regex as an argument, and not a String. $ is a special character an won't be refereed as a String. Solutions:
Use String#replace instead - It accepts a String and not a regex:
string.replace("<$200","<$200");
Use Pattern#quote - It returns a string representation:
string.replaceAll(Pattern.quote("<$200"),"<$200");
Escape special characters by adding \\ before the special characters.
Use this
String demo ="<$200";
demo = demo.replaceAll("<","<");
System.out.println(demo);

Split function not working properly

I am trying to split the string using Split function in java
String empName="employee name | employee Email";
String[] empDetails=empName.split("|");
it gives me result as
empDetails[0]="e";
empDetails[1]="m";
empDetails[2]="p";
empDetails[3]="l";
empDetails[4]="o";
empDetails[5]="y";
empDetails[6]="e";
empDetails[7]="e";
.
.
.
but when i try following code
String empName="employee name - employee Email";
String[] empDetails=empName.split("-");
it gives me
empDetails[0]="employee name ";
empDetails[1]=" employee Email";
why java split function can not split the string seperated by "|"
String#split() method accepts a regex and not a String.
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String[] empDetails=empName.split("\\|");
Update:
Handling special characters in java:OFFICIAL DOCS.
As a side note:
In java method names starts with small letters.it should be split() not Split() ..not the capital and small s
but my question is why we have to use escape in case of "|" and not for "-"
Because "|" is a regex meta-character. It means "alternation"; e.g. "A|B" means match "A" or "B". If you have problems understanding Java regexes, the javadocs for Pattern describe the complete Java regex syntax.
So when you split on "|" (without the escaping!), you are specifying that the separator is "nothing or nothing", and that matches between each character of the target string.
(For the record, "-" is also a meta-character, but only in a "[..]" character group. In other contexts it doesn't require escaping.)
You should use .split("\\|"); instead of .split("|");
Try
String[] empDetails=empName.split("\\|");

Categories