How to replace ' with empty string in Java - java

How can I replace single quote ' with empty string in Java. I tried following but doesn't seem to be working.
String data="Sid's den";
data.replace("'", "");
data.replaceAll("'", "");
Thanks in advance. Any help is much appreciated.(Output should be: Sids den)
Thanks guys for your responses. I guess I should have been more clear about my question. Basically I am getting special characters from the table and with what value we have to replace that also from the same table. Here is snippet of the code:
query = "select spl_char, replace_with from entcon_splchars";
ptsmt = DBConnector.sqlConnection.prepareStatement(query);
rs = ptsmt.executeQuery();
while (rs.next()) {
if(data.contains(rs.getString("spl_char"))){
data = data.replace(rs.getString("spl_char"),rs.getString("replace_with"));
}
}
so whenevr in the data we have special character ' then I am facing nullpointer exception. Please suggest how to go ahead with this?

Use replace, no need for regex.
Remember that Strings are immutable, so you need to assign data.replace("'", ""); to a variable.
For instance: data = data.replace("'", "");

Strings are immutable so you'll receive a new instance. Try
data = data.replace("'", "");
Your Edit
Check the return values of getString() - you could get your NPE because your database table contains null values in one of the columns spl_char or replace_with.

I can see your problem, If you need to replace it you need to replace it and also need to assign it back to the variable. Solution should be,
String data="Sid's den";
data = data.replaceAll("'", "");
System.out.println(data);
Because String is immutable in java. But StringBuffer and StringBuilder is mutable in java.
String
StringBuffer
StringBuilder

Just try with:
"foo'bar'buz".replace("'", "")
Output:
"foobarbuz"
In your case:
String data = "Sid's den";
String output = data.replace("'", "");

Try:
data = data.replace ("'", "") ;
OR
data = data.replaceAll("'", "") ;
You would need to assign the replaced string to a variable.

Related

Replace multiple substrings within a string

I have string such as
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3"
I would like to replace the "substrings" "v1","v2" and "v3" with other values. How can I achieve this?
Does something like this work for your case?
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3";
String result = String.format(url.replaceAll("v[0-9]", "%s"), "arg1", "arg2", "arg3");
System.out.println(result); //www.test.com/blabla/?p1=arg1?p2=arg2?p3=arg3
Edit:
Just a brief explanation of what this does, it replaces all the v1,v2,v3,v4,v5,v6,v7,v8,v9,v0 in the original url for %s and then uses this in the format method so you can attribute what you want it to be.

Java String TRIM function not working

I am receiving a string from server trailing one or two lines of spaces like below given string.
String str = "abc*******
********";
Consider * as spaces after my string
i have tried a few methods like
str = str.trim();
str = str.replace(String.valueOf((char) 160), " ").trim();
str = str.replaceAll("\u00A0", "");
but none is working.
Why i am not able to remove the space?
You should try like this:
str = str.replaceAll("\n", "").trim();
You can observe there is a new line in that string . first replace new line "\n" with space("") and than trim
You should do:
str = str.replaceAll("\n", "");
In my case use to work the function trim()
Try this:
str = str.replaceAll("[.]*[\\s\t]+$", "");
I have tried your 3 methods, and them all work. I think your question describing not correctly or complete, in fact, a String in java would not like
String str = "abc*******
********";
They must like
String str = "abc*******"
+ "********";
So I think you should describe your question better to get help.

How to remove a substring in java

I am receiving a file path with "xyz" appended to it. name would look like D:/sdcard/filename.docxyz
i am using the below code to remove xyz but it is not working. what is missing here ?
String fileExtension = path.substring(path.lastIndexOf(".")+1);
String newExtension= fileExtension;
newExtension.replace("xyz", "");
path.replace(fileExtension, newExtension);
return path;
What is missing is that you need to save the result of your operations. Strings are immutable in Java, and the results of all String manipulations are therefore returned in the form of a new String:
newExtension = newExtension.replace("xyz", "");
path = path.replace(fileExtension, newExtension);
String in java are immutable, and changes upon it never occurs in place, but every time a new string is returned,
newExtension = newExtension.replace("xyz", "");
You could also use replaceAll() with a regex.
public static void main(String[] args) {
String s = "D:/sdcard/filename.docxyz";
System.out.println(s.replaceAll("xyz$", "")); // $ checks only the end
}
O/P :
input : s = "D:/sdcard/filename.docxyz";
D:/sdcard/filename.doc
input : String s = "D:/sdcard/filenamexyz.docxyz";
output : D:/sdcard/filenamexyz.doc
newExtension.replace("xyz", "");
Will only return string which has "xyz" removed but newExtension will remain as it is. Simple fix for your problem is use as below
String newExtension= fileExtension.replace("xyz", "");

Using regular expressions to rename a string

In java, I want to rename a String so it always ends with ".mp4"
Suppose we have an encoded link, looking as follows:
String link = www.somehost.com/linkthatIneed.mp4?e=13974etc...
So, how do I rename the link String so it always ends with ".mp4"?
link = www.somehost.com/linkthatIneed.mp4 <--- that's what I need the final String to be.
Just get the string until the .mp4 part using the following regex:
^(.*\.mp4)
and the first captured group is what you want.
Demo: http://regex101.com/r/zQ6tO5
Another way to do this would be to split the string with ".mp4" as a split char and then add it again :)
Something like :
String splitChar = ".mp4";
String link = "www.somehost.com/linkthatIneed.mp4?e=13974etcrezkhjk"
String finalStr = link.split(splitChar)[0] + splitChar;
easy to do ^^
PS: I prefer to pass by regex but it ask for more knowledge about regex ^^
Well you can also do this:
Match the string with the below regex
\?.*
and replace it with empty string.
Demo: http://regex101.com/r/iV1cZ8
Try below code,
private String trimStringAfterOccurance(String link, String occuranceString) {
Integer occuranceIndex = link.indexOf(occuranceString);
String trimmedString = (String) link.subSequence(0, occuranceIndex + occuranceString.length() );
System.out.println(trimmedString);
return trimmedString;
}

Problem in replacing special characters in a String in java

I got a String as response from server which is like the below:
hsb:\\\10.217.111.33\javap\Coventry\
Now I want to parse this string in such a way that I need to replace all \ with /.
Also I need to remove the first part of the String which is hsb:\\\
So, my resultant string should be of like this :
10.217.111.33/javap/coventry/
Can anyone help me by providing sample java code for this problem.
Here you have a "dirty" startup "solution":
String s = "hsb:\\\\\\10.217.111.33\\javap\\Coventry\\";
String w = s.replace('\\', '/');
String x = w.replace("hsb:///", "");
String result = yourString.substring(7);
result = result.replaceAll("\\\\", "/");

Categories

Resources