首页 新闻 会员 周边

String字符串截取

1
悬赏园豆:10 [已解决问题] 解决于 2016-09-06 11:07
String str = "房估字(2014)第YPQD0001号,房估字(2014)第YPQD00022号,房估字(2014)第YPQD000333号,房估字(2014)第YPQD0004444号";
如何截取第和号中间的所有值,别保存在list中呢?中间的值每个都不一样
柔情似水丶的主页 柔情似水丶 | 初学一级 | 园豆:37
提问于:2016-09-01 10:35
< >
分享
最佳答案
1
string str = "房估字(2014)第YPQD0001号,房估字(2014)第YPQD00022号,房估字(2014)第YPQD000333号,房估字(2014)第YPQD0004444号";
Regex reg = new Regex(@"(YPQD\d{4,10})");
var match = reg.Matches(str);
List<string> result = new List<string>();
 foreach (Match item in match)
{
    result.Add(item.ToString());
}  

 

收获园豆:10
liuxb1991 | 小虾三级 |园豆:661 | 2016-09-01 10:50
其他回答(8)
1

正则(?<=房).*?(?=号)

吴瑞祥 | 园豆:29449 (高人七级) | 2016-09-01 10:40

看不懂

支持(1) 反对(0) 荆棘人 | 园豆:410 (菜鸟二级) | 2016-09-01 11:02

@荆棘人: 正则表达式啊.找出前面是房后面是号的中间一段....

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2016-09-01 11:37

@荆棘人: 看不懂没事,会用就行。

支持(0) 反对(0) 小刺猬001 | 园豆:660 (小虾三级) | 2016-09-03 12:59
1

如果是C#就简单了。。。

str.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries).Select(i=>这里面写处理代码).ToList()

 

其中相关的处理代码可能用正则,可能用自己代码,其实自己用indexof等相关代码就可以满足要求。。。

另外,如果不是C#,而是Java,也就是按正常思路,先split,然后再依次删掉不需要的东西吧。。。

顾晓北 | 园豆:10844 (专家六级) | 2016-09-01 10:45
0
import java.beans.IntrospectionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    
    public static void main(String[] args) throws IntrospectionException {

        String str = "房估字(2014)第YPQD0001号,房估字(2014)第YPQD00022号,房估字(2014)第YPQD000333号,房估字(2014)第YPQD0004444号";
        Pattern p = Pattern.compile("(?<=第).+?(?=号)");
        Matcher matcher = p.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        } 
    }

楼上 + 1

之奇一昂 | 园豆:1421 (小虾三级) | 2016-09-01 10:59
0

String str = "房估字(2014)第YPQD0001号,房估字(2014)第YPQD00022号,房估字(2014)第YPQD000333号,房估字(2014)第YPQD0004444号";

String[] str_1 = str.split("号");
List<String> list = new ArrayList<>();
for (String s : str_1) {
list.add(s.substring(s.indexOf("第") + 1));
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}

柔情似水丶 | 园豆:37 (初学一级) | 2016-09-01 11:34
0

上面给的答案都不错。

坤坤 | 园豆:919 (小虾三级) | 2016-09-01 13:11
0

上面 的答案已经可以解决

爱吃de馒头 | 园豆:255 (菜鸟二级) | 2016-09-01 15:55
0

大神常出没,答案有几个了。

雨之秋水 | 园豆:649 (小虾三级) | 2016-09-02 11:35
0

大家好热情,不仅解决了 ,还给出了好几个解决方案。

小刺猬001 | 园豆:660 (小虾三级) | 2016-09-03 13:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册