两个字符之间的子串 [英] Substring between two characters

查看:94
本文介绍了两个字符之间的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串::

I have a string like this::

";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;"





我需要回来



I need to get back

abc def xyz



即介于两者之间;并且:



我只能回到abc ..但不能def和xyz ..我试过这个::


i.e which is in between ; and :

I am only able to get back abc..but not def and xyz.. I tried this::

string abc = ";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";
            
                int firstpos = abc.IndexOf(';');
                int secondpos = abc.IndexOf(':');

                string result = abc.Substring(firstpos + 1, secondpos - 1);

推荐答案

尝试这样的事情:

Try something like this:
string abc = ";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";

Regex pattern = new Regex(";(?<val>[^;:]+?):", RegexOptions.ExplicitCapture);

IEnumerable<string> values = pattern.Matches(abc).Cast<Match>().Select(m => m.Groups["val"].Value);


我需要回来只是意味着你使用了错误的方法:使用表示数据的字符串而不是数据本身。您应该传递原始数据结构的所有位置,而不是表示此数据结构的字符串。好吧,如果你在主机之间传递这个结构,你可能需要使用数据的序列化,但在这种情况下学习和使用序列化(可以是字符串或二进制)。



-SA
"I need to get back" simply means that you are using wrong approach: using strings representing data instead of data itself. You should pass everywhere your original data structure, not a string representing this data structure. Well, if you pass this structure between hosts, you may need to use serialization of the data, but in this case learn and use serialization (which can be string or binary).

—SA


这篇关于两个字符之间的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆