我有一组名字,我需要将它们组合成逗号分隔的字符串。
生成的字符串需要遵循正确的语法。
如果集合包含一个名称,则输出应该只是该名称:
John
如果集合包含两个名称,那么输出应该用单词“and”分隔:
John and Mary
如果集合包含三个或更多名称,则输出应以逗号分隔,并且姓氏前面应包含单词“and”:
John, Mary, and Jane
这是我提出的代码。它不是很优雅,我想知道在C#中是否有更好的方法来实现这一点(4.0可以)。
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
string names = string.Empty;
for (int i = 0; i < firstNames.Count; i++)
{
if (i == 1 && firstNames.Count == 2)
{
names += " and ";
}
else if (firstNames.Count > 2 && i > 0 && i != firstNames.Count - 1)
{
names += ", ";
}
else if (i != 0 && i == firstNames.Count - 1)
{
names += ", and ";
}
names += firstNames[i];
}
答案 0 :(得分:6)
我认为它不会比以下更优雅:
if (names.Count == 0)
return "";
if (names.Count == 1)
return names[0];
if (names.Count == 2)
return names[0] + " and " + names[1];
return String.Join(", ", names.Take(names.Count - 1)) +
", and " + names[names.Count - 1];
我没有编译这个,但我想你明白了。
编辑:更短,但不太可读:
if (names.Count <= 2)
return String.Join(" and ", names);
return String.Join(", ", names.Take(names.Count - 1)) +
", and " + names[names.Count - 1];
答案 1 :(得分:3)
我会做这样的事情:
using System;
using System.Collections.Generic;
class Program {
static void Main(string[] args) {
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
Console.WriteLine(NamesString(firstNames));
}
static string NamesString(List<string> firstNames) {
switch (firstNames.Count) {
case 0:
return string.Empty;
case 1:
return firstNames[0];
case 2:
return string.Join(" and ", firstNames.ToArray());
default:
return string.Format("{0} and {1}",
string.Join(", ", firstNames.ToArray(), 0, firstNames.Count - 1),
firstNames[firstNames.Count - 1]);
}
}
}
编辑:实际应该是
default:
return string.Format("{0}, and {1}",
如果您想在最后一个“和”之前使用逗号。
答案 2 :(得分:2)
string a=string.Join(",", FirstNames.ToArray());
if (FirstNames.Count == 1)
a.Replace(",", "");
else if (FirstNames.Count == 2)
a.Replace(",", " and ");
else
{
int i = a.LastIndexOf(",");
a = a.Substring(1, i) + a.Substring(i).Replace(",", " and ");
}
答案 3 :(得分:1)
static string JoinNames(List<string> firstNames)
{
int count = firstNames.Count;
if(count == 1)
{
return firstNames[0];
}
if(count > 1)
{
return string.Join(", ", firstNames.Take(count - 1).ToArray()) + " and " + firstNames[count - 1];
}
return string.Empty;
}
答案 4 :(得分:0)
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
StringBuilder names = new StringBuilder();
for (int i = 0; i < firstNames.Count; i++)
{
if((i-1) == firstNames.Count && names.length > 0)
names.AppendFormat(" and {0}", names[i]);
else if(names.length > 0)
names.AppendFormat(", {0}", names[i]);
}
return names.ToString();
我认为这是最好的方式。
答案 5 :(得分:0)
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
int cnt = 0;
string names = string.Empty;
while (cnt<=firstName.Count)
{
string separator = "";
if (firstName.Count - cnt > 1) separator = ", ";
else if (firstName.Count - cnt = 1) separator = ", and ";
else separator = "";
names += firstName[cnt] + separator;
cnt += 1;
}
答案 6 :(得分:0)
这是一个有趣的解决方案:
//code
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
StringBuilder sb = new StringBuilder();
firstNames.Take(firstNames.Count - 1).ToList().ForEach(fn => AddString(fn, sb));
sb.Append(", and " + firstNames.Last());
//Helper Function
public void AddString (string fn, StringBuilder sb)
{
if (sb.Length != 0)
{
sb.Append(", ");
}
sb.Append(fn);
}
答案 7 :(得分:0)
尝试使用此尺寸:
List<string> firstNames = new List<string>();
firstNames.Add("John");
firstNames.Add("Mary");
firstNames.Add("Jane");
// Only do this if there is more than one name
if (firstNames.Count > 1)
{
string separator = ", ";
// Join the names, using ", " as a separator
string names = String.Join(seperator, firstNames.ToArray());
// Insert "and" before the last comma
names = names.Insert(names.LastIndexOf(separator), ", and ");
// Remove the last comma
names = names.Remove(names.LastIndexOf(separator), separator.Length);
}
答案 8 :(得分:0)
var a = String.Join(", ", firstNames.Skip(1).ToArray()) + (firstNames.Count < 2 ? "" : ", and ") + firstNames.Take(1).FirstOrDefault();
答案 9 :(得分:0)
我更喜欢最具可读性的代码。在列表中添加名称时,您需要考虑4个标点符号,因此请将其拼写出来。
static string Combine(List<string> names)
{
var sb = new StringBuilder();
for (int i = 0; i < names.Count; i++)
{
if (i == 0) //at start of a list
{}
else if (i < names.Count - 1) //in middle of list
sb.Append(", ");
else if( names.Count == 2 ) //at end of a list with 2 elements
sb.Append(" and ");
else //at end of a list with 3 or more elements
sb.Append(", and ");
sb.Append(names[i]);
}
return sb.ToString();
}
答案 10 :(得分:0)
public string Combine(List<string> names)
{
if (names == null || names.Count == 0)
{
return string.Empty;
}
var sb = new StringBuilder();
sb.Append(names[0]);
//Handle Special Case of 2 names
if (names.Count == 2)
{
sb.Append(" and " + names[1])
return sb.ToString();
}
for (int i = 1; i < names.Count; i++)
{
if (i == (names.Count -1))
{
sb.Append(", and " + names[i]);
}
else
{
sb.Append(", " + names[i]);
}
}
return sb.ToString();
}