java给定前后字符串如何获取中间字符串

invoked 2010-10-20 01:31:24

如题
...全文
450 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
千杯不醉-sen 2010-10-20
  • 打赏
  • 举报
回复
给两个下标,遍历即可。
fei860417 2010-10-20
  • 打赏
  • 举报
回复
来学习下咯,谢谢各位的分享啊
xuyisen_1987 2010-10-20
  • 打赏
  • 举报
回复
package com.xuyisen.socket.socket;

public class Test {

/**
* 截取字符串source中begin到end之间的字符串
* @param source 源字符串
* @param begin 开始
* @param end 结束
* @return
*/
public String subString(String source,String begin, String end){
String result = "";
if(!source.contains(begin)){
result = "源字符串中没有字符串: " + begin;
return result;
}else if(!source.contains(end)){
result = "源字符串中没有字符串: " + end;
return result;
}
int beginIndex = source.indexOf(begin);
int endIndex = source.indexOf(end);
result = source.substring(beginIndex+begin.length(), endIndex);
return result;
}

public static void main(String[] args) {
Test t = new Test();

String str = "louzhugeiwojiafen,ok";
String begin = "louzhu";
String end = ",ok";

String s = t.subString(str, begin, end);
System.out.println(s);
}
}
约翰羊 2010-10-20
  • 打赏
  • 举报
回复
路过蹭分......
invoked 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 blazingfire 的回复:]

Java code
public static void main(String[] args) {
String s = "楼主你给我100分!";
String mid = s.replaceAll(".*你(.*)!.*", "$1");
System.out.println(mid);
}
[/Quote]

赞,可惜给不了那么多
blazingfire 2010-10-20
  • 打赏
  • 举报
回复
    public static void main(String[] args) {
String s = "楼主你给我100分!";
String mid = s.replaceAll(".*你(.*)!.*", "$1");
System.out.println(mid);
}
invoked 2010-10-20
  • 打赏
  • 举报
回复
非常好了,想用Python语言实现,那区蛮冷的,谢谢大家。
小9 2010-10-20
  • 打赏
  • 举报
回复
	static String getStr(String src,String beg,String end){
String regex = beg+"(.*)"+end;
System.out.println(regex);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(src);
if(m.find()){
return m.group(1);
}

return null;
}
public static void main(String[] args)
{
System.out.println(getStr("aaa12121212dde", "aaa", "dde"));
}


要是有特殊字符,那要麻烦点,简单的就是这样
parckle 2010-10-20
  • 打赏
  • 举报
回复
楼主真大方!赞一个
24K純帥 2010-10-20
  • 打赏
  • 举报
回复

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="nimabi";
String pre="ni";
String suf="bi";
System.out.println(s.substring((s.indexOf(pre)+pre.length()), s.indexOf(suf)));
}

}
closewbq 2010-10-20
  • 打赏
  • 举报
回复

String str="aaaaxxxxxxxxbbbb";
String begin="aaaa";
String end="bbbb";
Matcher m=Pattern.compile("^"+begin+"(.*)"+end+"$").matcher(str);
if(m.find())
System.out.println(m.group(1));
凉岑玉 2010-10-20
  • 打赏
  • 举报
回复
subString(s.length()/4,(s.length()/4)*3)

62,610

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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