如何在java中清除某个字符后的所有字符串

我想把某个字符串中的某个字符之后的所有字符串全部删除。
如:
aaa.html
bbb.html?id=9
ccc.html?id=10&&name=zhangsan
想把指定的字符串"."以后的所有字符全部去掉
我想得到的是aaa,bbb和ccc
请问如何实现,在线等...........

首先获取字符的位置 i
nt loc = str.indexOf("字符");//首先获取字符的位置

然后调用字符串截取
String newStr = str.substring(0,loc);//再对字符串进行截取,获得想要得到的字符串
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-08
String str = "ccc.html?id=10&&name=zhangsan";
int idx = str.indexOf(".");
str = str.substring(0, idx);本回答被提问者采纳
第2个回答  2009-03-25
public class Test98 {
public static void main(String[] args) {
String s1 = "aa.html";//你应该是获得的地址,我将他们放入字符串
String s2 = "bbb.html?id=9 ";
String s3 = "ccc.html?id=10&&name=zhangsan";
test(s1);//测试
test(s2);
test(s3);
}

public static void test(String tmp) {//截取方法
tmp.substring(0,tmp.indexOf(".html"));
System.out.println(tmp);
}
}
第3个回答  2009-03-25
你应该仔细看看API,String有个substring()方法~~~
String a = "aaaa.html";
a = a.substring(0, a.indexOf("."));
a = "aaaa";
第4个回答  2009-03-25
String a = "ccc.html?id=10&&name=zhangsan";
a = a.substring(0,a.indexOf("."));

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网