String.Split方法帮助 [英] String.Split method help

查看:99
本文介绍了String.Split方法帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用string.Split方法分解字符串,但我遇到了一堵砖墙。



字符串将永远在格式:

FirstName姓氏,(位置)

例如:

John Doe,(销售员)



我已成功使用此代码获取名字和姓氏:



Hi I am trying to break up a string by using the string.Split method but I have run into a brick wall.

The string Will always be in the format of:
FirstName LastName, (Position)
For Example:
John Doe, (Sales Clerk)

I have succeded in getting the firstname and lastname by using this code:

string inputString = "John Doe, (Sales Clerk)";
string fstName = inputString.Split(' ')[0].Replace(" ", "");
string lstName = inputString.Split(' ')[1].Replace(",", "");





我不能做的是获取括号中的任何内容(位置)。



位置值可能包含一个像(Sales Clerk)这样的空间,它需要能够识别。



我正在考虑获取')'字符的索引并使用for循环从char之前循环到'('之后的char之后的字符串然后把它变成字符串变量。但是我不确定如何做到这一点。



但是,这是我的方法之一,可能有一种不同的方式使用String.Split方法,但我会看到论坛社区可以提出什么。 />


提前感谢所有帮助。



来自,

NattyMan0007



What I can't do is get whatever is in the Brackets (Position).

The position Value might contain a space like (Sales Clerk) it will need to be able to identify.

I was thinking of getting the index of the ')' Character and using for loop to loop from the char before until the char after the index of '(' and then bing that to a string variable. However I am unsure as to how to do this.

That, however, is one of my methods and there probably a different way of doing it using the String.Split method but I'll see what the forum community can come up with.

Thanks for all the help, in advance.

From,
NattyMan0007

推荐答案

代码没有意义。如果用分隔,则不能仅仅因为找不到该字符而替换。您可以通过检测以('和另一个以')'开头的情况来识别剩余的术语。这很简单(我不明白为什么你会用这么简单的逻辑失败:-)),但也没什么意义。



你可以解决这个特殊问题使用Regex更有效: http://msdn.microsoft.com/en- us / library / system.text.regularexpressions.regex.aspx [ ^ ]。



当然,您需要学习如何编写和使用这些正则表达式:

http://en.wikipedia.org/wiki/Regular_Expression [ ^ ],

http://msdn.microsoft.com/en-us/library/hs600312.aspx [ ^ ],

http://www.dmoz.org/Computers/Programming/Languages/Regular_Expressions/ [ ^ ]。



无论如何这都很有用,而且很简单,所以不会浪费时间。



但是现在,更深层次的问题是:你为什么收到结构数据是如此非结构形式的单一字符串?我宁愿审查你的架构/设计。为什么不使用XML序列化(或其他序列化,JSON,二进制)?或者你可能只需要直接使用一些结构?不幸的是,今天初学者最大的谬误之一是使用表示数据而不是数据的字符串。尽量避免它。



-SA
The code makes no sense. If you split by " ", you cannot replace " " simply because this character won't be found. You can identify the remaining terms by detecting the case when one starts with '(' and another ends with ')'. This is simple (I fail to understand why would you fail with such simple logic :-)), but also makes little sense.

You can solve this particular problem much more effectively by using Regex: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

Of course, you will need to learn how to write and use those Regular Expressions:
http://en.wikipedia.org/wiki/Regular_Expression[^],
http://msdn.microsoft.com/en-us/library/hs600312.aspx[^],
http://www.dmoz.org/Computers/Programming/Languages/Regular_Expressions/[^].

This will be useful anyway, and simple enough, so it would not be a waste of time.

But now, the deeper problem is: why do you receive structural data in such a non-structural form as a single string? I would rather review your architecture/design. Why would not you use XML serialization (or other serialization, JSON, binary)? Or maybe you just need to use some structures directly? Unfortunately, one of the biggest fallacies of the beginners this days is using strings representing data instead of data. Try to avoid it by all means.

—SA


这是使用Split的另一种选择;这将处理具有任意数量的空格的position条目,或者是一个连续的字符串:
Here's one alternative using Split; this will handle the "position" entry having any amount of white-space, or being one continuous string:
private string fName, lName, position;

private string[] splitStr;

private char[] splitChars = new char[] { ' ', '('};

private void GetInputData(string inputString)
{
    splitStr = inputString.Split(splitChars);

    fName = splitStr[0];
    lName = splitStr[1].Replace(",", "");

    position = "(";
    for (int i = 2; i < splitStr.Length; i++)
    {
        position += splitStr[i];
    }
}

但是,是的:一个不错的RegEx会更加优雅:)

But, yes: a nice RegEx would be so much more elegant :)


我的头顶,正则表达式解决方案为此:

Off the top of my head, the Regex solution for this:
private static readonly Regex FullNamePattern = new Regex(@"^(\w+) +(\w+), +\((.+)\)


这篇关于String.Split方法帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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