String.endsWith() not working - java

I have the following string
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
I want it so if the user forgets to input the http:// or the .PDF, the program will automatically correct this. Therefore, I tried this code
if (!str.startsWith("http://")) { // correct forgetting to add 'http://'
str = "http://" + str;
}
System.out.println(str);
if (!str.endsWith("\\Q.PDF\\E")) {
str = str + "\\Q.pdf\\E";
}
However, even when I enter the correct string, http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
the output is this.
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF\Q.pdf\E
Why? Why is another '.PDF' being added?

Because http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF doesn't have a \Q.PDF\E on the end. In a string literal, \\ gives you a backslash. So "\\Q.PDF\\E" is \Q.PDF\E — a backslash, followed by a Q, followed by a dot, followed by PDF, followed by another backslash, followed by E.
If you want to see if the string ends with .PDF, just use
if (!str.endsWith(".PDF"))
Of course, that's case-sensitive. If you want it to be case-insensitive, probably:
if (!str.toLowerCase().endsWith(".pdf"))

Hy. I think this is what you want:
String str = "http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP";
//String str = "http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF";
if (!str.startsWith("http://")) { // correct forgetting to add 'http://'
str = "http://" + str;
}
System.out.println(str);
if (!str.endsWith(".PDF")) {
str = str + ".PDF";
}
System.out.println(str);
}

- Its simply because your String http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF doesNot ends with \Q.PDF\E
- If you are concerned with matching the .PDF, then do this...
if (s.endsWith(".PDF")){
// add it at the end....
}
- It would be more appropriate to use StringBuilder here instead of String, which is mutable.

Related

java find and replace %

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);

substring between two delimiters

I have a string as : "This is a URL http://www.google.com/MyDoc.pdf which should be used"
I just need to extract the URL that is starting from http and ending at pdf :
http://www.google.com/MyDoc.pdf
String sLeftDelimiter = "http://";
String[] tempURL = sValueFromAddAtt.split(sLeftDelimiter );
String sRequiredURL = sLeftDelimiter + tempURL[1];
This gives me the output as "http://www.google.com/MyDoc.pdf which should be used"
Need help on this.
This kind of problem is what regular expressions were made for:
Pattern findUrl = Pattern.compile("\\bhttp.*?\\.pdf\\b");
Matcher matcher = findUrl.matcher("This is a URL http://www.google.com/MyDoc.pdf which should be used");
while (matcher.find()) {
System.out.println(matcher.group());
}
The regular expression explained:
\b before the "http" there is a word boundary (i.e. xhttp does not match)
http the string "http" (be aware that this also matches "https" and "httpsomething")
.*? any character (.) any number of times (*), but try to use the least amount of characters (?)
\.pdf the literal string ".pdf"
\b after the ".pdf" there is a word boundary (i.e. .pdfoo does not match)
If you would like to match only http and https, try to use this instead of http in your string:
https?\: - this matches the string http, then an optional "s" (indicated by the ? after the s) and then a colon.
why don't you use startsWith("http://") and endsWith(".pdf") mthods of String class.
Both the method returns boolean value, if both returns true, then your condition succeed else your condition is failed.
Try this
String StringName="This is a URL http://www.google.com/MyDoc.pdf which should be used";
StringName=StringName.substring(StringName.indexOf("http:"),StringName.indexOf("which"));
You can use Regular Expression power for here.
First you have to find Url in original string then remove other part.
Following code shows my suggestion:
String regex = "\\b(http|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
String str = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
String[] splited = str.split(regex);
for(String current_part : splited)
{
str = str.replace(current_part, "");
}
System.out.println(str);
This snippet code cans retrieve any url in any string with any pattern.
You cant add customize protocol such as https to protocol part in above regular expression.
I hope my answer help you ;)
public static String getStringBetweenStrings(String aString, String aPattern1, String aPattern2) {
String ret = null;
int pos1,pos2;
pos1 = aString.indexOf(aPattern1) + aPattern1.length();
pos2 = aString.indexOf(aPattern2);
if ((pos1>0) && (pos2>0) && (pos2 > pos1)) {
return aString.substring(pos1, pos2);
}
return ret;
}
You can use String.replaceAll with a capturing group and back reference for a very concise solution:
String input = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
System.out.println(input.replaceAll(".*(http.*?\\.pdf).*", "$1"));
Here's a breakdown for the regex: https://regexr.com/3qmus

Making a song name out of a URL

I have a URL and I want it to look like this:
Action Manatee - Action
http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3
What is the syntax for trimming up to where it after this "Stream/" and make spaces where the %20 is. I also want to trim the .mp3
Hmm, for that particular example, I would split the string according to the '/' character then trim the text that follows the final '.' character. Finally, do a replace of "%20" into " ". That should leave you with the string you want
Tested
String initial = "http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3";
String[] split = initial.split("/");
String output = split[split.length-1];
int length = output.lastIndexOf('.');
output = output.substring(0, length);
output = output.replace("%20", " ");
String urlParts[] = URL.split("\/");
String urlLast = urlParts[length-1];
String nameDotMp = urlLast.replaceAll("%20");
String name = nameDotMp.substring(0,nameDotMp.length-5);
You could use the split() and replace() methods to accomplish this, here are two ways:
Split your string apart by using the forward slashes:
string yourUrl = [URL Listed];
//Breaks your URL into sections on slashes
string[] sections = yourUrl.split("\/");
//Grabs the last section after the slashes, and replaces the %20 with spaces
string newString = sections[sectiongs.length-1].replace("%20"," ");
Split your string at the Stream/ section: (Only use this if you can guarantee it will be in that form)
string yourUrl = [URL Listed];
//This will get everything after Stream (your song name)
string newString = yourUrl.split("Stream\/")[1];
//Replaces your %20s with spaces
newString = newString.replace("%20"," ");
URL songURL = new URL("yourpath/filename");
String filename = songURL.getFile();

removing space before new line in java

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());
}
}

How to remove " " from java string

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 "&nbsp" 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 :)

Categories