Removing series of character in a String - java

I need to remove all the character in a String after a "?" mark like this:
http://xyz.com//static/css/style.css?v=e9b34
However I can't just spit it with "?", I need to make sure that the pattern is something .xyz?any_char_or_symbols
In other words extension-?-any_chars_or_symbols
Anyone can give a hint?

return s.replaceFirst("(\\.\\w+\\?).*$", "$1");

Am I missing something here or this does it:
String s = "http://xyz.com//static/css/style.css?v=e9b34";
int index = s.indexOf('?');
if (index<0)
return null;
String returned = s.substring(0,index);
if (returned.endsWidth(".xyz");
return returned;
else
return null;

First Remove the characters after ? then check for extentions.
String url="http://xyz.com//static/css/style.css?v=e9b34" ;
url=url.substring(0, url.indexOf("?"));
System.out.print(""+url.matches("^[\\w\\d\\:\\/\\.]+\\.\\w{3}(\\?[\\w\\W]*)?$"));
url.matches will check for the extention. I have defined it for 3 words w{3} but you can increase it as w{3,5}. now it will accepts 3 to 5 words extentions. if you want specific formats then use this patteren.
url.matches("^[\\w\\d\\:\\/\\.]+\\.(?i)(css|txt|html)?$")
Hope it will help.

Related

Unable to replace String containing special characters

I want to replace all special characters with whitespace but I am unable to replace x :
String search = "640×20141007151608##$%$20141008104817.jpeg";
String newSearch = search.replaceAll("[\\p{Punct}&&[^_]]", "");
System.out.println(newSearch);
output : 640×2014100715160820141008104817jpeg
I use the logic below:
String newSearch = search.replaceAll("[^A-Za-z0-9 ]","");
That is, remove anything that is not a number or a digit. Is this what you wanted ?
[^0-9a-zA-Z\.]
Try this.Repalce by ``.See demo.
http://regex101.com/r/hQ1rP0/51

Determine a number from text string

I want determine number from specify string.
Ex: I have many text strings, such as "3.2p" or "3.2px" or "xp3.2" or "p3.2x".
The final result I want is can get number from text in above. Expected result "3.2".
People who know,
Please help me,
Thanks,
I would first remove all the non-numeric characters using a regex, then parse what remains.
String str = input.replaceAll("[^\\d.]", "");
Float.parseFloat(str);
Use this:
String s = "ffffa32.334tccy";
s = s.replaceAll("[^\\d.]", "");

How to get perticular digits from the string in android?

I have one String = GETMSG_m_m_5556 from this I want to read only 5556, means I want to read all the digits after last "_". The string has not fixed length of numbers it may be like, GETMSG_m_m_9898786589 OR GETMSG_m_m_98987865. So how can I read the numbers after "_"?
Can anyone suggest me the write way.? It may be foolish question but I am stuck on this. I cant get any idea about this.
Thanks in advance.
String digits = sampleString.subString(sampleString.lastIndexOf("_"),sampleString.lenght);
Get the last index of the char '_' in your string and make a required substring to get the numbers .see string.subString()
You can use the string.split() function and take last string.
String[] separated = yourString.split("_");
// Now choose the last array value
You can try using StringTokenizer
StringTokenizer st = new StringTokenizer(String);
while (st.hasMoreTokens()) {
String s = st.nextToken();
if (s.startsWith("_")) {
....
}}

How to replace || (two pipes) from a string with | (one) pipe

I am getting response for some images in json format within this tag:
"xmlImageIds":"57948916||57948917||57948918||57948919||57948920||57948921||57948‌ ​922||57948923||57948924||57948925||57948926||5794892"
What i want to do is to separate each image id using .split("||") of the string class. Then append url with this image id and display it.
I have tried .replace("\"|\"|","\"|"); but its not working for me. Please help.
EDIT: Shabbir, I tried to update your question according to your comments below. Please edit it again, if I didn't get it right.
Use
.replace("||", "|");
| is no special char.
However, if you are using split() or replaceAll instead of replace(), beware that you need to escape the pipe symbol as \\|, because these methods take a regex as parameter.
For example:
public static void main(String[] args) {
String in = "\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57948‌922||57948923||57948924||57948925||57948926||5794892\"".replace("||", "|");
String[] q = in.split("\"");
String[] ids = q[3].split("\\|");
for (String id : ids) {
System.out.println("http://test/" + id);
}
}
I think I know what your problem is. You need to assign the result of replace(), not just call it.
String s = "foo||bar||baz";
s = s.replace("||", "|");
System.out.println(s);
I tested it, and just calling s.replace("||", "|"); doesn't seem to modify the string; you have to assign that result back to s.
Edit: The Java 6 spec says "Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar." (the emphasis is mine).
According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html, replace() takes chars instead of Strings. Perhaps you should try replaceAll(String, String) instead? Either that, or try changing your String ("") quotation marks into char ('') quotation marks.
Edit: I just noticed the overload for replace() that takes a CharSequence. I'd still give replaceAll() a try though.
String pipe="pipes||";
System.out.println("Old Pipe:::"+pipe);
System.out.println("Updated Pipe:::"+pipe.replace("||", "|"));
i dont remember how it works that method... but you can make your own:
String withTwoPipes = "helloTwo||pipes";
for(int i=0; i<withTwoPipes.lenght;i++){
char a = withTwoPipes.charAt(i);
if(a=='|' && i<withTwoPipes.lenght+1){
char b = withTwoPipes.charAt(i+1);
if(b=='|' && i<withTwoPipes.lenght){
withTwoPipes.charAt(i)='';
withTwoPipes.charAt(i+1)='|';
}
}
}
I think that some code like this should work... its not a perfect answer but can help...

How to validate string using regex in java

I want to validate a string which donot have numeric characters.
If my string is "javaABC" then it must be validated
If my string is "java1" then it must not be validated
I want to restrict all the integers.
Try this:
String Text = ...;
boolean HasNoNumber = Text.matches("^[^0-9]*$");
'^[^0-9]*$' = From Start(^) to end ($), there are ([...]) only non(^) number(0-9). You can use '\D' as other suggest too ... but this is easy to understand.
See more info here.
You can use this:
\D
"\D" matches non-digit characters.
Here is one way that you can search for a digit in a String:
public boolean isValid(String stringToValidate) {
if(Pattern.compile("[0-9]").matcher(stringToValidate).find()) {
// The string is not valid.
return false;
}
// The string is valid.
return true;
}
More detail is here:
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
The easiest to understand is probably matching for a single digit and if found fail, instead of creating a regexp that makes sure that all characters in the string are non-digits.

Categories

Resources