请问我现在有两个字符串,分别是“(-3.1 ~ 3.9)”和“(0 ~ 6)”怎么从这个字符串里把-3.1和3.9取出来?要分开在两个变量里,例如a=-3.1 b=3.9 谢谢!求大神解答
string ss = "(-3.1 ~ 3.9)和“(0 ~ 6)”";
var dd = ss.Split('和')
.Select(ee => ee.Split("()()~“”".ToArray(), StringSplitOptions.RemoveEmptyEntries))
.ToArray();
dd = "(-3.1 ~ 3.9)和“(0 ~ 6)”".Split('和')
.Select(ee => ee.Split("()()~“”".ToArray())
.Where(ff => ff != "").ToArray()).ToArray();
直接转为数值:
string ss = "(-3.1 ~ 3.9)和“(0 ~ 6)”";
var dd = ss.Split('和')
.Select(ee => ee.Split("()()~“”".ToArray(), StringSplitOptions.RemoveEmptyEntries)
.Select(ff => double.Parse(ff)).ToArray())
.ToArray();
dd = "(-3.1 ~ 3.9)和“(0 ~ 6)”".Split('和')
.Select(ee => ee.Split("()()~“”".ToArray())
.Where(ff => ff != "").Select(ff => double.Parse(ff)).ToArray()).ToArray();
这个贴使我想起了初学时写的等效取出数字的思路,寻这个思路写了如下一样可以实现:
var 取出指定的字符 = "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
.Where(dd => "0123456789.-".Contains(dd)).ToArray();
var 过滤指定的字符 = "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
.Where(dd => !"0123456789.-".Contains(dd)).ToArray();
var 数字组 = "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
.Split(过滤指定的字符).Where(dd => dd != "").ToArray()
.Select(dd => double.Parse(dd)).ToArray();
MessageBox.Show(string.Join("", 取出指定的字符) + "\r\n\r\n"
+ string.Join("", 过滤指定的字符) + "\r\n\r\n"
+ string.Join("\r\n", 数字组), "友情提示:");
循环实现:
var ff = ""; List<double> aa = new List<double>();
var 取出指定的字符 = "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
.Select(dd => "0123456789.-".Contains(dd) ? ff += dd : ff = "").ToArray();
foreach (var dd in "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz")
if ("0123456789.-".Contains(dd)) ff += dd;
else if (ff != "") { aa.Add(double.Parse(ff)); ff = ""; }
var 取指定的字符 = from dd in "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
where "0123456789.-".Contains(dd) select dd;
"qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz"
.Select(dd =>
{
if ("0123456789.-".Contains(dd)) ff += dd;
else if (ff != "") { aa.Add(double.Parse(ff)); ff = ""; }
return 0;
}).ToArray();
char[] 取滤数字字符(string 数据, bool 选 = true) { return 数据.Where(字 => "0123456789.-".Contains(字) == 选).ToArray(); }
string 字符 = "qwer12345yuio09876okmnj-12yhbgfvtr1.2345edcxswqaz";
var 数字组 = 字符.Split(取滤数字字符(字符, false)).Where(符 => 符 != "").ToArray()
.Select(dd => double.Parse(dd)).ToArray();
/// <summary>从一字符串中获取或滤指定的字符</summary>
/// <param name="数据">要从中获取或过滤指定字符的字符串。</param>
/// <param name="选">默认为真时为获取指定字符,假时为过滤指定字符。</param>
/// <param name="字符">默认为空则使用函数内定义好了的数字字符,可以自定义任意指定的字符用于获取或过滤。</param>
/// <returns></returns>
char[] 取滤指定字符(string 数据, bool 选 = true, string 字符 = "") { return 数据.Where(字 => (字符 == "" ? "0123456789.-" : 字符).Contains(字) == 选).ToArray(); }