C#中的线处理
Line treatment in C#
我在字符串操作方面遇到了一些问题。我的输入中有一些字符串:
var firstString = "Endi Marrei";
var SecondString = "Spain";
我需要连接这两个字符串,例如:
Output = "Marrei E. (Spa)";
我试过这个:
var d = firstString.Split(' ');
var dd = firstString[0].ToString();
var Output = d[1] + " " + dd + "." +" " + "(" + "???" + ")";
如何将西班牙切到第三个元素?
也就是说,即使我的方法有效,你知道更干净的解决方案吗?
var firstString = "Endi Marrei";
var SecondString = "Spain";
var d = firstString.Split(' ');<br/>
var Output = d[1] + " " + d[0][0] + "." + " " + "(" + SecondString.Substring(0, 3) + ")";
d[0][0] 等同于 firstString[0].ToString();
even though your code is working
..所以我猜,您正在寻找一种更简洁的方法(而不是一个班轮)。简单的建议是不要使用 +
运算符来连接字符串和空格。尝试使用 String.Format
以获得更清晰的代码
var firstString = "Endi Marrei";
var SecondString = "Spain";
var parts = firstString.Split(' ');
var d = String.Format("{0} {1}.", parts[1], parts[0][0].ToString().ToUpper());
var dd = String.Format("({0})", SecondString.Substring(0, 3));
var output = String.Format("{0} {1}", d, dd);
你的问题是“你知道更干净的解决方案吗”。
嗯……我的回答是:
我假设您假设每个国家/地区的所有 3letterCode 都是由国家/地区的前 3 个字母创建的(如 SPAin => SPA
)...等等。方法不对,看这里:
http://www.worldatlas.com/aatlas/ctycodes.htm
返回代码:
public class CountriesFactory
{
private static List<Country> _countries = new List<Country>()
{
new Country("Spain", "Spa"),
new Country("Germany", "Ger")
}
public static Country GetCountryByName(string countryName)
{
return _countries.Where(p => p.CountryName == countryName).FirstOrDefault() ?? Country.NONE;
}
}
public class Country
{
public string CountryName { get; private set; }
public string ThreeLetterCode { get; private set; }
public const Country NONE = new Country("", "");
public Country (string countryName, string threeLetterCode)
{
this.CountryName = countryName;
this.ThreeLetterCode = threeLetterCode;
}
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public Country Country { get; set; }
public override string ToString()
{
return string.Format("{0} {1}. ({2})", Name, Surname[0].ToString(), Country.ThreeLetterCode);
}
}
有些条件需要补充,现在不重要了。
在代码中使用:
Person person = new Person()
{
Name = "Endi",
Surname = "Marrei",
Country = CountriesFactory.GetCountryByName("Spain")
}
Console.WriteLine(person.ToString());
以你为例:
var firstString = "Endi Marrei";
var SecondString = "Spain";
var person = new Person()
{
Name = firstString.Split(' ')[0].ToString(),
Surname = firstString.Split(' ')[1].ToString(),
Country = CountriesFactory.GetCountryByName(SecondString)
}
我认为我的方法将来会更有用。
您可以使用正则表达式,尽管这是否是一种更简洁的方法还有待商榷。
var match = Regex.Match(firstString + " " + secondString, @"(\w)\S*\s+(\w+)\s+(\w{3})");
var output = match.Success
? string.Format("{1} {0}. ({2})",
match.Groups[1], match.Groups[2], match.Groups[3])
: string.Empty;
Console.WriteLine(output);
// output: Marrei E. (Spa)
我在字符串操作方面遇到了一些问题。我的输入中有一些字符串:
var firstString = "Endi Marrei";
var SecondString = "Spain";
我需要连接这两个字符串,例如:
Output = "Marrei E. (Spa)";
我试过这个:
var d = firstString.Split(' ');
var dd = firstString[0].ToString();
var Output = d[1] + " " + dd + "." +" " + "(" + "???" + ")";
如何将西班牙切到第三个元素?
也就是说,即使我的方法有效,你知道更干净的解决方案吗?
var firstString = "Endi Marrei";
var SecondString = "Spain";
var d = firstString.Split(' ');<br/>
var Output = d[1] + " " + d[0][0] + "." + " " + "(" + SecondString.Substring(0, 3) + ")";
d[0][0] 等同于 firstString[0].ToString();
even though your code is working
..所以我猜,您正在寻找一种更简洁的方法(而不是一个班轮)。简单的建议是不要使用 +
运算符来连接字符串和空格。尝试使用 String.Format
以获得更清晰的代码
var firstString = "Endi Marrei";
var SecondString = "Spain";
var parts = firstString.Split(' ');
var d = String.Format("{0} {1}.", parts[1], parts[0][0].ToString().ToUpper());
var dd = String.Format("({0})", SecondString.Substring(0, 3));
var output = String.Format("{0} {1}", d, dd);
你的问题是“你知道更干净的解决方案吗”。
嗯……我的回答是:
我假设您假设每个国家/地区的所有 3letterCode 都是由国家/地区的前 3 个字母创建的(如 SPAin => SPA
)...等等。方法不对,看这里:
http://www.worldatlas.com/aatlas/ctycodes.htm
返回代码:
public class CountriesFactory
{
private static List<Country> _countries = new List<Country>()
{
new Country("Spain", "Spa"),
new Country("Germany", "Ger")
}
public static Country GetCountryByName(string countryName)
{
return _countries.Where(p => p.CountryName == countryName).FirstOrDefault() ?? Country.NONE;
}
}
public class Country
{
public string CountryName { get; private set; }
public string ThreeLetterCode { get; private set; }
public const Country NONE = new Country("", "");
public Country (string countryName, string threeLetterCode)
{
this.CountryName = countryName;
this.ThreeLetterCode = threeLetterCode;
}
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public Country Country { get; set; }
public override string ToString()
{
return string.Format("{0} {1}. ({2})", Name, Surname[0].ToString(), Country.ThreeLetterCode);
}
}
有些条件需要补充,现在不重要了。
在代码中使用:
Person person = new Person()
{
Name = "Endi",
Surname = "Marrei",
Country = CountriesFactory.GetCountryByName("Spain")
}
Console.WriteLine(person.ToString());
以你为例:
var firstString = "Endi Marrei";
var SecondString = "Spain";
var person = new Person()
{
Name = firstString.Split(' ')[0].ToString(),
Surname = firstString.Split(' ')[1].ToString(),
Country = CountriesFactory.GetCountryByName(SecondString)
}
我认为我的方法将来会更有用。
您可以使用正则表达式,尽管这是否是一种更简洁的方法还有待商榷。
var match = Regex.Match(firstString + " " + secondString, @"(\w)\S*\s+(\w+)\s+(\w{3})");
var output = match.Success
? string.Format("{1} {0}. ({2})",
match.Groups[1], match.Groups[2], match.Groups[3])
: string.Empty;
Console.WriteLine(output);
// output: Marrei E. (Spa)