Reputation: 43
I have a list of names (cyclists) in order of Lastname, Firstname. I want to run code So it puts Lastname in front of Firstname. The Lastname is always written in uppercase and can contain one more values. So i decided to string split to array, that works. Only putting it together is hard.
here is my code so far: (tried it with for and foreach)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string fullName = "BELAMONTE VALVERDE Allechandro Jesus";
string[] words = fullName.Split(' ');
foreach (string word in words)
if (word.ToUpper() == word)
{
string lastname = string.Join(" ", word);
Console.WriteLine(word);
}
Console.ReadLine();
string fullName2 = "GONZALEZ GALDEANO Igor Anton";
string[] words2 = fullName2.Split(' ');
for (int i = 0; i < words2.Length; i++)
{
string word2 = words2[i];
if (word2.ToUpper() == word2)
{
string lastname2 = string.Join(" ", word2);
Console.WriteLine(lastname2);
}
}
Console.ReadLine();
}
}
}
It gives a output like
BELAMONTE VALVERDE
BELAMONTE VALVERDE
I want that to be on one line. The actual use wil be read a record from a table convert that and Replace that for the loaded item.
Upvotes: 1
Views: 2022
Reputation: 7
string[] newArr = (from x in asda select x.ToUpper()).ToArray();
Upvotes: -1
Reputation: 3188
You have a code design problem here:
foreach (string word in words)
if (word.ToUpper() == word)
{
string lastname = string.Join(" ", word);
Console.WriteLine(word);
}
What you want to do is to write the lastname once, right? So let's split the algorithm:
string[] words = fullName.Split(' ');
We don't need to "save" the words thanks to a handy class named StringBuilder, so it would go like this:
string fullName = "BELAMONTE VALVERDE Allechandro Jesus";
string[] words = fullName.Split(' ');
StringBuilder sb = new StringBuilder();
foreach (string word in words)
if (word.ToUpper() == word)
{
sb.Append(word + " ");
}
else
break; // That's assuming you will never have a last name's part after a first name's part :p
if (sb.Length > 0)
sb.Length--; // removes the last " " added in the loop, but maybe you want it ;)
Console.WriteLine(sb.ToString());
Upvotes: -1
Reputation: 934
If you know that lastname will be all UPPERCASED and will be in front of first name, you can use regex for parsing uppercased letters and the rest of the name.
This is the regex:
([A-Z\s]+) (.*)
This one will match uppercased words where a space can be between them, that's the \s
([A-Z\s]+)
This one will match the rest of the name
(.*)
So the final code for switching one name could look like this:
static void Main(string[] args)
{
string fullName = "BELAMONTE VALVERDE Allechandro Jesus";
string pattern = @"([A-Z\s]+) (.*)";
var parsedName = Regex.Match(fullName,pattern);
string firstName = parsedName.Groups[2].ToString();
string lastName = parsedName.Groups[1].ToString();
string result = firstName + " " + lastName;
}
Upvotes: 0
Reputation: 19190
The first thing you want to do is encapsulate the logic that's testing whether part of a string is uppercase:-
Detecting if a string is all CAPS
public bool IsAllUppercase(string value)
{
return value.All(x => x.IsUpper);
}
Then you want to encapsulate the logic that's extracting the uppercase part of your name
public string GetUppercasePart(string value)
{
return string.Join(" ", value.Split(" ").Where(x => IsAllUppercase(x));
}
Then getting the uppercase part of the name is really simple:-
var lastName = GetUppercasePart("BELAMONTE VALVERDE Allechandro Jesus");
I get the impression, though, that there's more to your problem than just getting all of the uppercase words in a string.
WARNING: If this is code for a production application that you're going to run anywhere other than your computer, then you want to take into account that IsUpper
means different things in different locales. You might want to read up on how internationalisation concerns affect string manipulation:-
In C# what is the difference between ToUpper() and ToUpperInvariant()?
C# String comparisons: Difference between CurrentCultureIgnoreCase and InvariantCultureIgnoreCase
Upvotes: 3