Reputation: 192
I'm working on a method which takes a string and separates it into numerous items and then stores it inside of an array. I then perform a check to see if the array contains more than two items and if it does I assign specific items within an array to corresponding strings i.e. assign first item to forename, last item to surname and all other items to middle-name.
My issue relates to retrieving each of the items within the array which are not the first or last items and then assigning them in the correct order to a string. Below you can see that I have attempted to grab each item within the name array and assign the result to a list which can then be converted into a single string, but with no luck. What I am looking for is a solution to this problem.
public string SplitName(string text)
{
string forename;
string middlename;
string surname;
var name = text.Split(' ');
if (name != null)
{
if (name.Length > 2)
{
forename = name[0];
surname = name[name.Length - 1];
List<string> temp = new List<string>();
for (int i = 1; i < name.Length - 1; i++)
{
// Assign each item to middlename
}
text = string.Format("{0} {1} {2}", forename, middlename, surname);
}
}
else
{
text = "INVALID";
}
return text;
}
Upvotes: 0
Views: 261
Reputation: 437336
If all you want to do is keep the first and last tokens separately and treat everything else as the middle name it would be much simpler to use a regular expression:
var regex = new Regex(@"^(\S+)\s+(?:(.*?)(?:\s+)?)(\S+)$");
var match = regex.Match("foo bar baz");
if (match.Success) {
var firstName = match.Groups[1].Value;
var middleName = match.Groups[2].Value;
var lastName = match.Groups[3].Value;
}
else {
// there were no spaces, i.e. just one token
}
The regular expression matches two runs of non-space characters at the ends with \S+
. It optionally matches anything else that happens to be between them with the pattern (?:(.*?)(?:\s+)?)
, where the important part is (.*?)
(match anything, non-greedy in order to not munch up any spaces before the last name) and the rest is mostly red tape (non-capturing groups, optional spaces before the lastname).
Upvotes: 3
Reputation: 23087
This will add all middlenames to middlename separated with space and trim last space
for (int i = 1; i < name.Length - 1; i++)
{
middlename+=name[i]+" ";
}
middlename = middlename.Trim(); // just for sure
If you need something else please specify your question
if your name is
aa bb cc dd ee
midlename will be
bb cc dd
Upvotes: 2
Reputation: 838
First, you need to initialize the string variable, then you can just append all "middle" items to it.
public string SplitName(string text)
{
string forename = string.Empty;
string middlename = string.Empty;
string surname = string.Empty;
var name = text.Split(' ');
if (name != null)
{
if (name.Length > 2)
{
forename = name[0];
surname = name[name.Length - 1];
List<string> temp = new List<string>();
for (int i = 1; i < name.Length - 1; i++)
{
middlename += name[i];
}
text = string.Format("{0} {1} {2}", forename, middlename, surname);
}
}
else
{
text = "INVALID";
}
return text;
}
Upvotes: 2
Reputation: 8868
Try with this:
public string SplitName(string text)
{
string forename;
string middlename;
string surname;
var name = text.Split(' ');
if (name != null)
{
if (name.Length > 2)
{
forename = name[0];
surname = name[name.Length - 1];
middlename = string.Join(" ", name.Skip(1).Take(name.Length - 2));
text = string.Format("{0} {1} {2}", forename, middlename, surname);
}
}
else
{
text = "INVALID";
}
return text;
}
But this return exactly the same string.
Upvotes: 2
Reputation: 627
Use String.Join (http://msdn.microsoft.com/en-us/library/tk0xe5h0.aspx)
public string SplitName(string text)
{
string forename;
string middlename;
string surname;
var name = text.Split(' ');
if (name != null)
{
if (name.Length > 2)
{
forename = name[0];
surname = name[name.Length - 1];
middlename = string.Join(" ", name, 1, name.Length - 2);
text = string.Format("{0} {1} {2}", forename, middlename, surname);
}
}
else
{
text = "INVALID";
}
return text;
}
Change the first parameter of join (" ") if you want them to be joined using a different string. Currently a name "Bob Jack Willis Mills" will create a middlename "Jack Willis". If you want "Jack, Willis" use ", " as the separator.
Upvotes: 6