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];
}
CodePudding user response:
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();
CodePudding user response:
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}] ", ",$&");
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
CodePudding user response:
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, ","))