I have a java string with " " from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(" ","") and it doesn't seem to work.
Any ideas?
cleaned = cleaned.replace(" "," ");
cleaned = cleaned.replace("\u00a0","")
This is a two step process:
strLineApp = strLineApp.replaceAll("&"+"nbsp;", " ");
strLineApp = strLineApp.replaceAll(String.valueOf((char) 160), " ");
This worked for me. Hope it helps you too!
The same way you mentioned:
String cleaned = s.replace(" "," ");
It works for me.
There's a ready solution to unescape HTML from Apache commons:
StringEscapeUtils.unescapeHtml("")
You can also escape HTML if you want:
StringEscapeUtils.escapeHtml("")
Strings are immutable so You need to do
string = string.replaceAll(" ","")
You can use JSoup library:
String date = doc.body().getElementsByClass("Datum").html().toString().replaceAll(" ","").trim();
String.replace(char, char) takes char inputs (or CharSequence inputs)
String.replaceAll(String, String) takes String inputs and matches by regular expression.
For example:
String origStr = "bat";
String newStr = str.replace('a', 'i');
// Now:
// origStr = "bat"
// newStr = "bit"
The key point is that the return value contains the new edited String. The original String variable that invokes replace()/replaceAll() doesn't have its contents changed.
For example:
String origStr = "how are you?";
String newStr = origStr.replaceAll(" "," ");
String anotherStr = origStr.replaceAll(" ","");
// origStr = "how are you?"
// newStr = "how are you?"
// anotherStr = howareyou?"
We can have a regular expression check and replace HTML nbsp;
input.replaceAll("[\\s\\u00A0]+$", "") + "");
It removes non breaking spaces in the input string.
My solution is the following, and only this worked for me:
String string = stringWithNbsp.replaceAll("NNBSP", "");
Strings in Java are immutable. You have to do:
String newStr = cleaned.replaceAll(" ", "");
I encountered the same problem: The inner HTML of the element I needed had " " and my assertion failed.
Since the question has not accepted any answer,yet I would suggest the following, which worked for me
String string = stringwithNbsp.replaceAll("\n", "");
P.S : Happy testing :)
Related
In the follwing String
String toBeFormatted= "[[LngLatAlt{longitude=-7.125924901999952, latitude=33.831783175000055, altitude=NaN},
LngLatAlt{longitude=-5.401396163999948, latitude=35.92213140900003, altitude=NaN}]]"
1- I need to replace all "LngLatAlt{longitude=" with open bracket "["
2- also need to replace all the intermediate ", latitude=33.831783175000055, altitude=NaN}" with ",33.831783175000055]"
That way my string result :
"[[[-7.125924901999952,33.831783175000055],[-5.401396163999948,35.92213140900003]]]"
try it the following reg exp :
String regexTarget = "(\\[\\[LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget, "\\[\\[\\[");
String regexTarget0 = "(, altitude=NaN\\}, LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget0, "],\\[");
String regexTarget1 = "(, latitude=)";
toBeFormatted.replaceAll(regexTarget1, " ,");
String regexTarget2 = "(, altitude=NaN\\})";
toBeFormatted.replaceAll(regexTarget2, "]");
but it seems not working.
Thank you for your help.
try something like:
String result = toBeFormatted.replaceAll("LngLatAlt\\{longitude=([^,]+), latitude=([^,]+), ([^}]+)\\}", "[$1, $2]");
System.out.println(result);
In java 1.8.0
I am trying to replace %, but it is not matching
String str = "%28Sample text%29";
str.replaceAll("%29", "\\)");
str.replaceAll("%28", "\\(");
System.out.println("Replaced string is " + str);
I have tried all this Replace symbol "%" with word "Percent" Nothing worked for me. Thanks in Advance.
It's working.
You need re-assign to str
str = str.replaceAll("%29", "\\)");
str = str.replaceAll("%28", "\\(");
Jerry06's answer is correct.
But you could do this simply by using URLDecoder to decode those unicode value.
String s = "%28Hello World!%29";
s = URLDecoder.decode(s, "UTF-8");
System.out.println(s);
Will output :
(Hello World!)
The problem is that you misunderstood the usage of replaceall. It's for regex based replacements. What you need to use is the normal replace method like that:
String str = "%28Sample text%29";
str=str.replace("%29", "\\)"). replace("%28", "\\(");
System.out.println("Replaced string is " + str);
I have this string for example:
Username: tester1tt8e677 Password: b6a492e14c
I need to get out the username and the password only.
The password and user name are dynamically changing.
What is the best way doing it with Java, and how?
Thanks.
Do yourself a massive favour and get started on regular expressions. It will take more time than blindly copy-pasting an answer, but once you grasp the concept you can solve a huge number of problems with it.
You might want to check out this tutorial, which is java-specifiy and looks quite solid. Or just go ahead and google, you will find tons of information out there.
Once again - please do learn about regular expressions. You will not regret it.
Try using a Matcher with a regex:
String pattern = "Username: (\\.+?) Password: (\\.+?)";
Matcher matcher = Pattern.compile( pattern ).matcher();
matcher.find();
You can then get your username and password from the first and second group:
String u = matcher.group(1);
String p = matcher.group(2);
However this does not sound like a good way to do whatever you are doing and you might want to consider another approach.
You could use substring:
String String1 = "Username: testter1tt8e677";
System.out.println(String1.substring(0,10));
This should return "Username:"
So:
System.out.println(String1.substring(10));
returns "testter1tt8e677".
Try with below code:
String s1 = "Username:tester1tt8e677 Password:b6a492e14c";
// splitting String
String[] splitString = s1.split(" ");
for (String sp: splitString) {
// Again Splitting
String[] s2 = sp.split(":");
System.out.println(s2[1]);
}
Rather than think of how you can work your solution around your problem, see if it can be broken up.
What do we want to do? We want to get values out of a String.
How? Rather than removing what we don't want, let's take out what we do want.
You then make the solution a lot simpler for yourself.
str = "Username: tester1tt8e677 Password: b6a492e14c";
String[] splitStrings = str.split("\\s+");
System.out.println(splitStrings[1]); //Username
System.out.println(splitStrings[3]); //Password
String str="Username: tester1tt8e677 Password: b6a492e14c";
System.out.println(str.substring(0,str.indexOf(" ", str.indexOf(" ") + 1)).split(": ")[1]);
System.out.println(str.substring(str.indexOf(" ", str.indexOf(" ") + 1)+1,str.length()).split(": ")[1]);
Output:
tester1tt8e677
b6a492e14c
Well I have found my answer and it was pretty easy:
StringBuilder pass1 = new StringBuilder(pass).delete(0, 35);
String resultString = pass1.toString();
StringBuilder user1 = new StringBuilder(user).delete(0, 10);
StringBuilder user2 = new StringBuilder(user1).delete(14, 35);
String resultString = user2.toString();
Thanks anyway (-:
I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);
i have a space before a new line in a string and cant remove it (in java).
I have tried the following but nothing works:
strToFix = strToFix.trim();
strToFix = strToFix.replace(" \n", "");
strToFix = strToFix.replaceAll("\\s\\n", "");
myString.replaceAll("[ \t]+(\r\n?|\n)", "$1");
replaceAll takes a regular expression as an argument. The [ \t] matches one or more spaces or tabs. The (\r\n?|\n) matches a newline and puts the result in $1.
try this:
strToFix = strToFix.replaceAll(" \\n", "\n");
'\' is a special character in regex, you need to escape it use '\'.
I believe with this one you should try this instead:
strToFix = strToFix.replace(" \\n", "\n");
Edit:
I forgot the escape in my original answer. James.Xu in his answer reminded me.
Are you sure?
String s1 = "hi ";
System.out.println("|" + s1.trim() + "|");
String s2 = "hi \n";
System.out.println("|" + s2.trim() + "|");
prints
|hi|
|hi|
are you sure it is a space what you're trying to remove? You should print string bytes and see if the first byte's value is actually a 32 (decimal) or 20 (hexadecimal).
trim() seems to do what your asking on my system. Here's the code I used, maybe you want to try it on your system:
public class so5488527 {
public static void main(String [] args)
{
String testString1 = "abc \n";
String testString2 = "def \n";
String testString3 = "ghi \n";
String testString4 = "jkl \n";
testString3 = testString3.trim();
System.out.println(testString1);
System.out.println(testString2.trim());
System.out.println(testString3);
System.out.println(testString4.trim());
}
}