Getting rid of quotes in a string - java

Hi I have a string with quotation marks
String s = "\"hello\"";
and I was wondering how to get rid of the quotes in the string s. Is there a method that can get rid of a specific char? Something like s.getRidofChar(");

Is this what you are looking for:
s = s.replaceAll("\"", "");
The \ is because we have to escape the quotes. This is saying we will replace all instances of quotes with the empty string. Remember that String is immutable, so we need to assign the value returned by replaceAll back to s (or some other variable).

String s = "\"hello\"";
s = s.replaceAll("\"","")

Edit:
Try replaceAll method.

You can use String.replaceAll() with an empty replacement string.

If you want to remove a certain character at a certain position, take the substring before and after the character and join them together.

Related

I am trying to remove double quotes from "234521" and get the output as 234521 in java

I have a CSV file in which the values are like this:
"12342","red","world"
For processing my code which is in java, I want the double quotes to be removed and assign it to a particular variable. Like this:
String number = num.replaceAll("^\"|\"$","");
Note:quotes will always be present in starting and in the end of the value.
But the output of number is "12342" instead of 12342. What should I write to replace those double quotes?
Thanks in advance!
String number = num.replaceAll("[^\\p{IsDigit}\\p{IsAlphabetic}.,]", "");
This will work with all Strings regardless they're numbers or just text. The regex replaces everything that's not a Digit nor Alphabetic, so will remove the quotes from the CSV fields. Doesn't remove . or , neither.
Alternative should be, for just the quotes:
String number = num.replace("\"", "");
You'd need the backslash \ to escape the double-quotes.
The below regular expression can also be used.
String number= num.replaceAll("^\"|\"$", "");

Java Regex to select starting and ending quotes in a string

I am inserting a string into a database and need to eliminate the beginning and ending quotes in the string.
I am using:
String a = rows[0].replaceAll("^\"", "").replaceAll("\"$", "");`
This gives me: "134432534"23" => 134432534"23 but is there a better way to do this?
I really do not see how your call to replace is typically bad here, but you could simplify this.
String a = rows[0].replaceAll("^\"|\"$", "");
To remove the first character and last character from the string, considering they are always quotes, use..
String a = rows[0].substring(1, rows[0].length()-1);

How to remove white spaces on either side of '=' sign?

I have the following string String string = "attr1 = 45 attr2 =\"82\"";
I am trying to remove all whitespace characters on either side of the = sign.
So that for example my output looks like:
attr1=45 attr="82"
I have tried the following:
String string = "attr1 = 45 attr2 =\"82\"";
string = string.replaceAll("\\s+", "");
I get the following output: attr1=45attr2="82"
Any suggestions would be appreciated.
You don't want to replace all spaces, but only these that are around =. Try with
string = string.replaceAll("\\s*=\\s*", "=");
Note that you cannot do this with just one regex since regexes are designed to match strings, not modify them. Regexes are often used with other tools to perform the later task. In particular, you can use String.replace() or String.replaceAll() with a very simple regex to accomplish your task.
Edit:
If you are still stumped, step back for a minute and think: How would you replace an equal sign with an asterisk, for example? Now can you modify that to do what you actually want?

Remove comments between quotes in String

I need to remove comments from a String. Comments are specified using quotes. For example:
removeComments("1+4 'sum'").equals("1+4 ");
removeComments("this 'is not a comment").equals("this 'is not a comment");
removeComments("1+4 'sum' 'unclosed comment").equals("1+4 'unclosed comment");
I could iterate through the characters of the String, keeping track of the indexes of the quotes, but I would like to know if there is a simpler solution (maybe a regex?)
You can use replaceAll:
str = str.replaceAll("\\'.*?\\'", "");
This will replace the first and the second ' and everything between them with "" (Thus, will remove them).
Edit: As stated on the comments, there is no need to backslash the single quote.
If you don't need to be able to have quotes inside the comment, this will do it:
input.replaceAll("'[^']+'", "");
It matches a quote, at least one of anything that isn't a quote, then a quote.
Working example

String replace a Backslash

How can I do a string replace of a back slash.
Input Source String:
sSource = "http://www.example.com\/value";
In the above String I want to replace "\/" with a "/";
Expected ouput after replace:
sSource = "http://www.example.com/value";
I get the Source String from a third party, therefore I have control over the format of the String.
This is what I have tried
Trial 1:
sSource.replaceAll("\\", "/");
Exception
Unexpected internal error near index 1
\
Trial 2:
sSource.replaceAll("\\/", "/");
No Exception, but does not do the required replace. Does not do anything.
Trial 3:
sVideoURL.replace("\\", "/");
No Exception, but does not do the required replace. Does not do anything.
sSource = sSource.replace("\\/", "/");
String is immutable - each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable)
replaceAll(..) uses regex. You don't need that.
Try replaceAll("\\\\", "") or replaceAll("\\\\/", "/").
The problem here is that a backslash is (1) an escape chararacter in Java string literals, and (2) an escape character in regular expressions – each of this uses need doubling the character, in effect needing 4 \ in row.
Of course, as Bozho said, you need to do something with the result (assign it to some variable) and not throw it away. And in this case the non-regex variant is better.
Try
sSource = sSource.replaceAll("\\\\", "");
Edit : Ok even in stackoverflow there is backslash escape... You need to have four backslashes in your replaceAll first String argument...
The reason of this is because backslash is considered as an escape character for special characters (like \n for instance).
Moreover replaceAll first arg is a regular expression that also use backslash as escape sequence.
So for the regular expression you need to pass 2 backslash. To pass those two backslashes by a java String to the replaceAll, you also need to escape both backslashes.
That drives you to have four backslashes for your expression! That's the beauty of regex in java ;)
s.replaceAll ("\\\\", "");
You need to mask a backslash in your source, and for regex, you need to mask it again, so for every backslash you need two, which ends in 4.
But
s = "http://www.example.com\\/value";
needs two backslashes in source as well.
This will replace backslashes with forward slashes in the string:
source = source.replace('\\','/');
you have to do
sSource.replaceAll("\\\\/", "/");
because the backshlash should be escaped twice one for string in source one in regular expression
To Replace backslash at particular location:
if ((stringValue.contains("\\"))&&(stringValue.indexOf("\\", location-1)==(location-1))) {
stringValue=stringValue.substring(0,location-1);
}
sSource = StringUtils.replace(sSource, "\\/", "/")

Categories

Resources