1

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.

Tezzo
  • 43
  • 6
  • 1
    I would like to help you but... I really can't understand what you want to achieve, sorry;o Is "BELAMONTE VALVERDE Allechandro Jesus" only one person?... Anyway where's this list with names that you are talking about. – rosko May 13 '14 at 12:48
  • @rosko I guess it is one person because there is a cyclist with that name :) Same for the other name – Dávid Kaya May 13 '14 at 12:52
  • @rosko Yes, That is one name. here is a part of the list. http://www.uci.ch/templates/BUILTIN-NOFRAMES/Template3/layout.asp?MenuId=MTYzMzc&LangId=1 – Tezzo May 13 '14 at 12:54
  • @Tezzo you want to put lastname in front of firstname? Isn't that the same as you got from your list? – Dávid Kaya May 13 '14 at 12:58
  • Btw your code gives the following output: BELAMONTE\nVALVERDE not the one you specified in your question;o – rosko May 13 '14 at 13:02
  • @Tezzo I have edited your question. You can changed input/output to make it simple for understanding. – Hassan May 13 '14 at 13:04
  • @David Kaya, true. made a big typo there – Tezzo May 13 '14 at 13:14
  • @Tezzo so you want first name in front of last name? – Dávid Kaya May 13 '14 at 13:15
  • @David Kaya Yes, that will be the whole trick. but would not ask that in advance because i have to learn and puzzle also. – Tezzo May 13 '14 at 13:18
  • @Tezzo I will add answer for switching last and first name – Dávid Kaya May 13 '14 at 13:19

4 Answers4

3

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

Community
  • 1
  • 1
Iain Galloway
  • 18,669
  • 6
  • 52
  • 73
  • it is, the problem is bigger, but i also have to learn and puzzle. Thanx. (The whole trick would be read from a database, convert to "Fname + Lname" and update that in the database.) – Tezzo May 13 '14 at 13:35
0

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;
}
Dávid Kaya
  • 924
  • 4
  • 17
-1

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:

  1. Get all words from the string: done string[] words = fullName.Split(' ');
  2. Read first word, if it's uppercase, save it
  3. Repeat 2 for the next word until it isn't uppercase
  4. Join all the saved words
  5. Print the result

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());
Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • You could simplify this further by turning the `foreach` loop into a LINQ query, and using `String.Join` rather than the `StringBuilder` – Ben Aaronson May 13 '14 at 12:58
  • Keepin' it old school – Kilazur May 13 '14 at 12:59
  • Seems like a valid answer. – rosko May 13 '14 at 13:03
  • Too late to edit my comment: you never know when you'll have to work on a <3.5 .NET project, so you'd better know how to do it old school style :p – Kilazur May 13 '14 at 13:11
  • I'd use String.Join over StringBuilder here regardless of LINQ or not e.g. I expect that `sb.Length--` will throw index out of range if the input has *no* uppercase part. – Iain Galloway May 13 '14 at 13:40
  • Just a little check to do, gonna edit. Overall, I'd rather use StringBuilder right ahead, even though it could turn me into a performance freak :p – Kilazur May 13 '14 at 13:58
  • Don't downvote without a reason. This is a valid answer AFAIK, with valid situations of use, even though it's not the most modern one. – Kilazur May 15 '14 at 08:15
-1

string[] newArr = (from x in asda select x.ToUpper()).ToArray();