Habip Oğuz
Habip Oğuz

Reputation: 1193

If all characters in the name is uppercase, put a comma in front of it

The names I use in the program are as follows: Jenny BLACK, Helen BROWN EYE, Henna Tamara GREEN. If the person has more than one first name or more than one surname, I want to put a comma just before the surname. How can I do that? We can distinguish that a string is a surname by the fact that all letters are capitalized.

With a method like this, maybe I can get what I want a very long way, but it doesn't sound professional at all.

string author_final = "";
var aut_examp = new List<string>{"Jenny BLACK", "Helen BROWN EYE", "Henna Tamara GREEN"};
if (aut_examp.Count > 1)
{
    var i = 1;
    foreach (var aut in aut_examp)
    {
        var and = i < aut_examp.Count ? "&" : "";

        var explodes = aut.Split(new[] { $" " }, StringSplitOptions.None);

        foreach (var ex in explodes)
        {
            if (ex.All(char.IsUpper) // ...
            // With a method like this, maybe I can get what I want a very long way, but it doesn't sound professional at all.
        }
        author_final += aut + and;
        i++;
    }
}
else
{
    author_final = aut_examp[0];
}

Upvotes: 0

Views: 285

Answers (3)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186718

You can try using regular expressions for the problem:

 using System.Text.RegularExpressions;

 ...

 string source = "Torin OAKENSHIELD";

 result = Regex.Replace(source, @"(?<!,)\s+[\p{Lu}]+[\s\p{Lu}]*", ",$&");

Here we use pattern (?<!,)\s+[\p{Lu}]+[\s\p{Lu}]* to match and then replace:

(?<!,)      - negative look - NOT started with comma
\s+         - followed by at least one (one or more) whitespace
[\p{Lu}]+   - followed by at least one capital letter
[\s\p{Lu}]* - followed by any number (zero or more) white spaces or 
              capital letters. 

In your case, you can try Linq and Regular expressions combination:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  var aut_examp = new List<string> {
    "Jenny BLACK", "Helen BROWN EYE", "Henna Tamara GREEN" };

  var demo = aut_examp
    .Select(name => Regex.Replace(name, @"(?<!,)\s+[\p{Lu}]+[\s\p{Lu}]+", ",$&"))
    .ToList();

  Console.WriteLine(string.Join(Environment.NewLine, demo));

Outcome:

Jenny, BLACK
Helen, BROWN EYE
Henna Tamara, GREEN

Upvotes: 1

Caius Jard
Caius Jard

Reputation: 74605

In LINQ you could break your names into words, then recombine them with "take while name contains lower case chars", a comma, "skip while name contains lower chars"

new [] {"Jenny BLACK", "Helen BROWN EYE", "Henna Tamara GREEN"}
.Select(n => 
  string.Join(" ", n.Split().TakeWhile(o => o.Any(Char.IsLower))) + 
  ", " + 
  string.Join(" ", n.Split().SkipWhile(o => o.Any(Char.IsLower)))
);

I think I'd rather just ask regex to find me the index of the first all uppercase word and insert a comma..

new [] {"Jenny BLACK", "Helen BROWN EYE", "Henna Tamara GREEN"}
.Select(n => n.Insert(Regex.Match(n, " [A-Z]+\\b").Index, ","))

Upvotes: 1

Prasad Telkikar
Prasad Telkikar

Reputation: 16059

You need to split names with space as a delimiter and then add comma for first upper case word.

Here are my 2 cents,

public static string GetProcessedName(string name)
{
    StringBuilder sb = new StringBuilder();
    //Flag to keep track of comma addition to output string.
    var commaAdded = false;
    foreach(var word in name.Split())
    {
        if(word == word.ToUpper() && !commaAdded)
        {
            sb.Append($", {word}");
            commaAdded = true;
        }
        else
           sb.Append(" "+word);
    }
    return sb.ToString();
}

Now call this method to the actual method,

var aut_examp = new List<string>{"Jenny BLACK", "Helen BROWN EYE", "Henna Tamara GREEN"};
var result = new StringBuilder();
foreach(var aut in examp)
   sb.AppendLine(GetProcessedName(aut));

string author_final = sb.ToString();

Try online : .NET Fiddle

Upvotes: 1

Related Questions