时间:2019-03-17 标签:c#substringindexof

发布于 2024-11-05 08:22:18 字数 419 浏览 8 评论 0原文

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

我只想从整个字符串中查找 Sam 和 Jones。

so string firstname = originalstring.substring ... 等等。

有谁知道我该怎么做?

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

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

因此,现在如果我计算对我没有帮助的字符数,则可能需要更多项目,而不仅仅是名字姓氏

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

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

so string firstname = originalstring.substring ... etc.

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 ....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥抱我好吗 2024-11-12 08:22:18

使用正则表达式

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;

请参阅此处的演示。

Use Regular expressions:

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;

See its Demo here.

筱果果 2024-11-12 08:22:18

我认为这可能比 .Split 方法更好。如果你有 ||在“Sam”和“LastName”之间,那么您肯定想要 .Split。事实上,这可能会更好。

    string inStr = "FirstName||Sam LastName||Jones Address||123 Main ST ";
    int fStart = inStr.IndexOf("FirstName") + "FirstName".Length + "||".Length;
    int fEnd = inStr.IndexOf(" LastName");

    string FirstName = inStr.Substring(fStart, fEnd - fStart);

I think this might work better than the .Split approach. If you had || between 'Sam' and 'LastName' then you'd certainly want to .Split. As it is, this might be better.

    string inStr = "FirstName||Sam LastName||Jones Address||123 Main ST ";
    int fStart = inStr.IndexOf("FirstName") + "FirstName".Length + "||".Length;
    int fEnd = inStr.IndexOf(" LastName");

    string FirstName = inStr.Substring(fStart, fEnd - fStart);
内心荒芜 2024-11-12 08:22:18

我会将字符串拆分两次,一次在“”上,然后在 || 上再次拆分。获取名字和姓氏的值

string [] ary = s.Split(" ");
string [] key;
string firstname;
string lastname;
foreach (string val in ary ) {
    key = val.Split("||");
    if ( key[0] == "FirstName") {
         firstname = key[1];
}
if ( key[0] == "LastName") {
   lastname = key[1];
}
}

I would split the string twice once on " " and then again on || to get the values of first and last name

string [] ary = s.Split(" ");
string [] key;
string firstname;
string lastname;
foreach (string val in ary ) {
    key = val.Split("||");
    if ( key[0] == "FirstName") {
         firstname = key[1];
}
if ( key[0] == "LastName") {
   lastname = key[1];
}
}
清风不识月 2024-11-12 08:22:18
str = Str.Split("||")[1].split(" ")[0] //Sam
str = Str.Split("||")[2].split(" ")[0] // Jones
str = Str.Split("||")[1].split(" ")[0] //Sam
str = Str.Split("||")[2].split(" ")[0] // Jones
熊抱啵儿 2024-11-12 08:22:18

像这样的 string firstname = originalstring.substring(indexof("FirstName") + 11, ((indexof("LastName) - indexof("FirstName") + 11 )

这样你就可以到达第一个|| 之后的字母,直到“姓氏”之前的第一个字母,姓氏也是如此,您只需将名字与姓氏和姓氏与地址

编辑:我的错...它不是 substring.(indexOf... 但它是

originalString.Substring(origianlString.IndexOf("FirstName) + 11, (originalString.IndexOf("LastName") - originalString.IndexOf("FirstName") + 11) 并且在查找最后一个时name 不是 + 11 而是 10,因为 "LastName".Length + + "||".Length = 10

something like this string firstname = originalstring.substring(indexof("FirstName") + 11, ((indexof("LastName) - indexof("FirstName") + 11 )

this way you get to the first letter after || and till the first letter before "lastname" the same goes for surname you just switch firstname with lastname and lastname with adress

edit: my fault... it's not substring.(indexOf... but it's

originalString.Substring(origianlString.IndexOf("FirstName) + 11, (originalString.IndexOf("LastName") - originalString.IndexOf("FirstName") + 11) and when looking for last name it's not + 11 but 10 because "LastName".Length + + "||".Length = 10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文