C#子字符串索引 [英] c# substring indexof

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

问题描述

我有一个看起来像这样的字符串- "FirstName||Sam LastName||Jones Address||123 Main ST ..."(另外100个不同的值)

i have a string which looks like this - "FirstName||Sam LastName||Jones Address||123 Main ST ..." (100 more different values)

我只想从整个字符串中找到Sam和Jones.

I want to find only Sam and Jones from the entire string.

所以字符串firstname = originalstring.substring ...等

有人知道我该怎么做吗?

Does anyone know how I can do this?

添加- 我想我忘了提及几件事.

ADDITION - I think i forgot to mention couple of things.

FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....

现在,如果我算不出多少字符,原因可能是除了 firstname lastname 之外,还需要更多项目.

So now if i count the number of characters that wont help me, cause could need more items other than just firstname and lastname.

推荐答案

使用正则表达式:

string myString = "FirstName||Sam LastName||Jones Address||123 Main ST...";
string pattern = @"FirstName\|\|(\w+) LastName\|\|(\w+) ";
Match m = Regex.Match(myString, pattern);
string firstName = m.Groups[1].Value
string lastName = m.Groups[2].Value;

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

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