Click here to Skip to main content
15,891,852 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:

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


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
Posted
Comments
Sergey Alexandrovich Kryukov 20-Dec-13 3:41am    
This is easy, but why?
—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
 
Share this answer
 
Comments
[no name] 20-Dec-13 3:49am    
The string is made from a SQL Query that gets actual values from a database and puts it into a readable format. The data is then displayed in a combobox so that the user can choose what value to edit. To avoid multiple matches when the database is queried to get the rest of the data to fill the textboxes on the form. This Way there are 3 seperate values that can be used to query the database to avoid multiple matches. If you have a better way of doing this please tell me so I can improve my product for sale quality standards.
Sergey Alexandrovich Kryukov 20-Dec-13 3:58am    
Then your database schema is really bad! You should have separate attributes (columns) for name components and other object properties in some table(s).
Anyway, I explained you what to do with a string. Are you going to accept the answer formally?
—SA
[no name] 20-Dec-13 4:01am    
I do have separate columns but the query is written so that it combines them in that format to shorten the amount of code that is needed
here's the query I am using:

SELECT (FirstName + ' ' + LastName + ', ' + '(' + EmployeePosition + ')') as FirstName from Employees

And thank you for your contribution

I might change it to store each in a seperate variable and that should make it easier.
Sergey Alexandrovich Kryukov 20-Dec-13 4:10am    
Then write a better query! :doh!:
—SA
[no name] 20-Dec-13 4:11am    
That was my plan...:D
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];
    }
}
But, yes: a nice RegEx would be so much more elegant :)
 
Share this answer
 
Off the top of my head, the Regex solution for this:
C#
private static readonly Regex FullNamePattern = new Regex(@"^(\w+) +(\w+), +\((.+)\)$", RegexOptions.Compiled);

      string fullname = "John Doe, (Sales Clerk)";
      Match matched = FullNamePattern.Match(fullname);
      string firstname = matched.Groups[1].Value;
      string lastname = matched.Groups[2].Value;
      string position = matched.Groups[3].Value;


In any case, changing the query to return the 3 columns as separate values would be a much simpler (and correct) solution.
 
Share this answer
 
v2
Comments
BillWoodruff 23-Dec-13 16:49pm    
+5 Now that, is elegant ! Hope you don't mind the minor edit.
Matt T Heffron 23-Dec-13 20:48pm    
Thanks!
 
Share this answer
 
iam sure this url will help you to solve this problem

http://www.dotnetperls.com/split
 
Share this answer
 
Comments
[no name] 20-Dec-13 3:28am    
I used this code:
string position = Regex.Split(inputString, "(")[0].Replace(")", "");
and it threw an exception:

An unhandled exception of type 'System.ArgumentException' occurred in System.dll

Additional information: parsing "(" - Not enough )'s.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900