java 中 如何截取

yibo2010 2010-08-14 07:32:35
String str = "aaa<2222>;bbb<3333>;cccc<5555>;cccc<666>;";

如何截取括号之间的值。
...全文
75 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
prototyper 2010-08-14
  • 打赏
  • 举报
回复
String str = "aaa<2222>;bbb<3333>;cccc<5555>;cccc<666>;";
Pattern peg = Pattern.compile("[^<]+(?=>)");
Matcher mat = peg.matcher(str);
while(mat.find()){ ................}
Cynthea 2010-08-14
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String s = "aaa<2222>;bbb<3333>;cccc<5555>;cccc<666>;";
String[] arr = s.split(";");
String[] arr1 = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
arr1[i] = "";
for (int j = 0; j < arr[i].length(); j++) {

if (arr[i].charAt(j)== '<'){

while(arr[i].charAt(++j) != '>') {
arr1[i] = arr1[i] + arr[i].charAt(j);
}
}

}
System.out.println(arr1[i]);
}

}

输出结果:
aaa<2222>
2222
bbb<3333>
3333
cccc<5555>
5555
cccc<666>
666
Cynthea 2010-08-14
  • 打赏
  • 举报
回复
分开保存到数组中之后,直接可以用<>括号去分离
Cynthea 2010-08-14
  • 打赏
  • 举报
回复
可以先用split(";")这个函数根据分号将其全部分开,保存在数组里面;
然后再用正则表达式去匹配<>之间的数字。

87,909

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧