So I'm playing around string manipulation. I'm done replacing white space characters with hyphens. Now I want to combine replacing white spaces characters and removing apostrophe from string. How can I do this?
This is what I've tried so far:
String str = "Please Don't Ask Me";
String newStr = str.replaceAll("\\s+","-");
System.out.println("New string is " + newStr);
Output is:
Please-Don't-Ask-Me
But I want the output to be:
Please-Dont-Ask-Me
But I can't get to work removing the apostrophe. Any ideas? Help is much appreciated. Thanks.
Try this:
String newStr = str.replaceAll("\\s+","-").replaceAll("'", "");
The first replaceAll returns the String with all spaces replaced with -, then we perform on this another replaceAll to replace all ' with nothing (Meaning, we are removing them).
It's very easy, use replaceAll again on the resulted String:
String newStr = str.replaceAll("\\s+","-").replaceAll("'","");
Try this..
String s = "This is a string that contain's a few incorrect apostrophe's. don't fail me now.'O stranger o'f t'h'e f'u't'u'r'e!";
System.out.println(s);
s = s.replace("\'", "");
System.out.println("\n"+s);
Related
I have a String as shown below:
String s = "A, Category, \"Agriculture, forestry and fishing\",";
I want to remove spaces around comma (which are outside quotes). So my string should look like:
A,Category,"Agriculture, forestry and fishing",
I tried following RE:
String s = s.replaceAll("[,]\\s+", ",");
but output is:
A,Category,"Agriculture,forestry and fishing",
What changes should I do in my regular expression to avoid changes for commas inside quotes?
You can use this :
String str = "A, Category, \"Agriculture, forestry and fishing\",";
String result = str.replaceAll(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "");
//------------------------------^--------------------------------------
System.out.println(result);
This will print :
A,Category,"Agriculture, forestry and fishing",
//----------------------^---------------------
The space inside the quotes is not changes, just outside the quotes.
Here is a code DEMO and here is a regex DEMO
EDIT
This part is from #Exception_al so he suggest to use this :
String result = str.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");
This solution is very good if you want to replace all spaces between comma , and the word.
You can check how this can work in regex DEMO
Problem:
Can't remove multiple white spaces in a string while working in eclipse editor
Context:
String myString1 ="aye bye tye ";
String myString2 =myString1.replaceAll("\\s+","");
System.out.println("replaced string ="+myString2);
In the output the white spaces are not removed and the result is the same as the string,
replaced string =aye bye tye
is getting printed
But if there is only one white space between the words like,
String myString1 ="aye bye tye";
the result is correctly coming as below:
replaced string =ayebyetye
I wonder where I am going wrong?
I can only guess that the spaces are not really space character (U+0020), but some Unicode space character, like U+00A0 NO BREAK SPACE. \s by default only matches space characters in the ASCII range, so they are not removed.
If you want to remove all Unicode spaces, you have to enable the UNICODE_CHARACTER_CLASS flag with inline construct (?U)
String myString2 = myString1.replaceAll("(?U)\\s+", "");
Use space in the replacement part so that one or more spaces would be replaced by a single space character.
String myString2 = myString1.replaceAll("\\s+", " ");
or
String myString2 = myString1.replaceAll("(\\s)+", "$1");
Why the
String myString2 =myString1.replaceAll(" ","");
Is not an option? You don't need a regex at all
I am getting this string from Db
str = "External - Internal ";
I want to remove the last whitespace from the string. I have already tried string.trim() and assigned it to another string
Kindly suggest as this is just not working. below is my code for reference.
public static void main(String args[]){
String str = "External - Internal ";
String temp = str.trim();
System.out.println("1"+temp);
temp=str.replaceAll(" ", "");
System.out.println("2"+temp);
temp=str.replace("\\r", "");
System.out.println("3"+temp);
}
Regards
Abhi
You could do this simply through string.replaceAll or string.replaceFirst function.
string.replaceAll("\\s(?=\\S*$)", "");
If you exactly mean the space which was at the end then use the below regex.
string.replaceAll("\\s$", "");
Use \\s+ instead of \\s if you want to deal with one or more spaces.
You can find your answer here Strip Leading and Trailing Spaces From Java String
Look at the top two answers. Try right trim as myString.replaceAll("\s+$", "");
i have a link http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area in my struts based web application.
The variable in URL justificationName have some spaces before its vales as shown. when i get value of justificationName using request.getParameter("justificationName") it gives me that value with spaces as given in the URL. i want to remove those spaces. i tried trim() i tries str = str.replace(" ", ""); but any of them did not removed those spaces. can any one tell some other way to remove the space.
Noted one more thing that i did right click on the link and opened the link into new tab there i noticed that link looks like.
http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName=%A0%A0%A0%A0%A0%A0%A0%A0No%20Technicians%20in%20Area
Notable point is that in the address bar it shows %A0 for white spaces and also show %20 for space as well see the link and tell the difference please if any one have idea about it.
EDIT
Here is my code
String justificationCode = "";
if (request.getParameter("justificationName") != null) {
justificationCode = request.getParameter("justificationName");
}
justificationCode = justificationCode.replace(" ", "");
Note: replace function remove the space from inside the string but not removing starting spaces.
e-g if my string is " This is string" after using replace it becomes " Thisisstring"
Thanks in advance
Strings are immutable in Java, so the method doesn't change the string you pass but returns a new one. You must use the returned value :
str = str.replace(" ", "");
Manual trim
You need to remove the spaces the string. This will remove any number of consecutive spaces.
String trimmed = str.replaceAll(" +", "");
If you want to replace all whitespace characters:
String trimmed = str.replaceAll("\\s+", "");
URL Encoding
You could also use an URLEncoder, which sounds like a more appropriate way to go:
import java.net.UrlEncoder;
String url = "http://localhost:8080/reporting/" + URLEncoder.encode("pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area", "ISO-8859-1");
You have to assign the result of the replace(String regex, String replacement) operation to another variable. See the Javadoc for the replace(String regex, String replacement) method. It returns a brand new String object and this is because the String(s) in Java are immutable. In your case, you can simply do the following
String noSpacesString = str.replace("\\s+", "");
You can use replaceAll("\\s","") It will remove all white space.
If you are trying to remove the trailing and ending white spaces, then
s = s.trim();
Or if you want to remove all the spaces the use :
s = s.replace(" ","");
There are two ways of doing one is regular expression based or your own way of implementing the logic
replaceAll("\\s","")
or
if (text.contains(" ") || text.contains("\t") || text.contains("\r")
|| text.contains("\n"))
{
//code goes here
}
String s ="SSR/DANGEROUS GOODS AS PER ATTACHED SHIPPERS
/DECLARATION 1 PACKAGE
NFY
/ACME CONSOLIDATORS"
How to strip the space between "PACKAGE" and "NFY" ?
Java's String.replaceAll in fact takes a regular expression. You could remove all newlines with:
s = s.replaceAll("\\n", "");
s = s.replaceAll("\\r", "");
But this will remove all newlines.
Note the double \'s: so that the string that is passed to the regular expression parser is \n.
You can also do this, which is smarter:
s = s.replaceAll("\\s{2,}", " ");
This would remove all sequences of 2 or more whitespaces, replacing them with a single space. Since newlines are also whitespaces, it should do the trick for you.
Try this code:
s = s.replaceAll( "PACKAGE\\s*NFY", "PACKAGE NFY" );
s = s.replaceAll("[\\n\\r]", "");
Have you tried a replace function? Something in the lines of:
youString.Replace("\r", "")
string = string.replace(/\s{2,}/g, ' ');