简体   繁体   中英

Removing series of character in a String

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?

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;
return s.replaceFirst("(\\.\\w+\\?).*$", "$1");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM