سؤال

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.

هل كانت مفيدة؟

المحلول 3

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());

نصائح أخرى

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

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;
}

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top