制作更短的替换、拆分和转换 C#
Make shorter replace, splitting and conversion C#
如何让我的代码更短?这是我的输入(它是坐标):
(51.01407864998378, 4.15557861328125)
我希望坐标分为两个不同的变量。
这是我的代码:
StringBuilder text = new StringBuilder(); // creation of stringbuilder
text.Append("(51.01407864998378, 4.15557861328125)"); //append my string
text.Replace("(", ""); // remove the round brackets and white space.
text.Replace(")", "");
text.Replace(" ", "");
text.Replace(",", "_"); //in europe we use a comma as separator for decimal numbers, not a point.
text.Replace(".", ",");
string[] parts = text.ToString().Split('_'); // splitting and convert it.
decimal lng = Convert.ToDecimal(parts[0]);
decimal lat = Convert.ToDecimal(parts[1]);
变量 lng
中的结果为 51.01407864998378,lat
中的结果为 4.15557861328125。
谁能缩短这段代码?我使用 C#。
谢谢
您可以使用正则表达式。
var coord = "(51.01407864998378, 4.15557861328125)";
Match match = Regex.Match(coord, @"\(([\d]*.[\d]*),[^\d]*([\d]*.[\d]*)\)$", RegexOptions.IgnoreCase);
if (match.Success)
{
decimal lng = Convert.ToDecimal(match.Groups[1].Value, CultureInfo.GetCultureInfo("en-US"));
decimal lat = Convert.ToDecimal(match.Groups[2].Value, CultureInfo.GetCultureInfo("en-US"));
Console.WriteLine(lng);
Console.WriteLine(lat);
}
结果
51.01407864998378
4.15557861328125
如果您尝试提取值,最短的方法是使用正则表达式,例如
\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)
通过使用命名捕获组,您可以按名称提取特定部分。
之后,您只需解析指定 InvariantCulture 的文本,确保 .
用作小数点分隔符,例如:
var coordRegex=new Regex(@"\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)");
var input = "(51.01407864998378, 4.15557861328125)";
var match = coordRegex.Match(input);
if (match.Success)
{
var lonText=match.Groups["lon"].Value;
var latText=match.Groups["lat"].Value;
var lon=decimal.Parse(lonText,CultureInfo.InvariantCulture);
var lat=decimal.Parse(latText,CultureInfo.InvariantCulture);
}
Regex.Match
是线程安全的,这意味着您可以创建单个静态 Regex 对象并避免重建该对象,例如:
static readonly Regex _coordRegex=new Regex(@"\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)");
正则表达式也更快,因为它避免 生成临时字符串,直到您使用Value
请求特定组。在那之前,Match
对象只包含指向原始字符串的指针。这意味着更少的对象分配、更少的内存压力和更少的垃圾收集。
这会在处理具有多个坐标的文件时带来显着的性能提升。在这种情况下,程序的大部分内存使用都是由于生成的临时对象造成的。
可以拆分的更简单,使用decimal.Parse
不关心点和逗号:
string src = "(51.01407864998378, 4.15557861328125)";
var temp = src.Split(new[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries);
var lng = decimal.Parse(temp[0], CultureInfo.InvariantCulture);
var lat = decimal.Parse(temp[1], CultureInfo.InvariantCulture);
如何让我的代码更短?这是我的输入(它是坐标):
(51.01407864998378, 4.15557861328125)
我希望坐标分为两个不同的变量。
这是我的代码:
StringBuilder text = new StringBuilder(); // creation of stringbuilder
text.Append("(51.01407864998378, 4.15557861328125)"); //append my string
text.Replace("(", ""); // remove the round brackets and white space.
text.Replace(")", "");
text.Replace(" ", "");
text.Replace(",", "_"); //in europe we use a comma as separator for decimal numbers, not a point.
text.Replace(".", ",");
string[] parts = text.ToString().Split('_'); // splitting and convert it.
decimal lng = Convert.ToDecimal(parts[0]);
decimal lat = Convert.ToDecimal(parts[1]);
变量 lng
中的结果为 51.01407864998378,lat
中的结果为 4.15557861328125。
谁能缩短这段代码?我使用 C#。
谢谢
您可以使用正则表达式。
var coord = "(51.01407864998378, 4.15557861328125)";
Match match = Regex.Match(coord, @"\(([\d]*.[\d]*),[^\d]*([\d]*.[\d]*)\)$", RegexOptions.IgnoreCase);
if (match.Success)
{
decimal lng = Convert.ToDecimal(match.Groups[1].Value, CultureInfo.GetCultureInfo("en-US"));
decimal lat = Convert.ToDecimal(match.Groups[2].Value, CultureInfo.GetCultureInfo("en-US"));
Console.WriteLine(lng);
Console.WriteLine(lat);
}
结果
51.01407864998378
4.15557861328125
如果您尝试提取值,最短的方法是使用正则表达式,例如
\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)
通过使用命名捕获组,您可以按名称提取特定部分。
之后,您只需解析指定 InvariantCulture 的文本,确保 .
用作小数点分隔符,例如:
var coordRegex=new Regex(@"\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)");
var input = "(51.01407864998378, 4.15557861328125)";
var match = coordRegex.Match(input);
if (match.Success)
{
var lonText=match.Groups["lon"].Value;
var latText=match.Groups["lat"].Value;
var lon=decimal.Parse(lonText,CultureInfo.InvariantCulture);
var lat=decimal.Parse(latText,CultureInfo.InvariantCulture);
}
Regex.Match
是线程安全的,这意味着您可以创建单个静态 Regex 对象并避免重建该对象,例如:
static readonly Regex _coordRegex=new Regex(@"\((?<lon>\d+\.\d+),\s+(?<lat>\d+\.\d+)\)");
正则表达式也更快,因为它避免 生成临时字符串,直到您使用Value
请求特定组。在那之前,Match
对象只包含指向原始字符串的指针。这意味着更少的对象分配、更少的内存压力和更少的垃圾收集。
这会在处理具有多个坐标的文件时带来显着的性能提升。在这种情况下,程序的大部分内存使用都是由于生成的临时对象造成的。
可以拆分的更简单,使用decimal.Parse
不关心点和逗号:
string src = "(51.01407864998378, 4.15557861328125)";
var temp = src.Split(new[] { '(', ',', ')' }, StringSplitOptions.RemoveEmptyEntries);
var lng = decimal.Parse(temp[0], CultureInfo.InvariantCulture);
var lat = decimal.Parse(temp[1], CultureInfo.InvariantCulture);