Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析字符串并仅返回括号符号之间的信息。C#Winforms_C#_Winforms - Fatal编程技术网

解析字符串并仅返回括号符号之间的信息。C#Winforms

解析字符串并仅返回括号符号之间的信息。C#Winforms,c#,winforms,C#,Winforms,我想解析一个字符串,只返回括号符号之间的值,例如[10.2%]。然后我需要去掉“%”符号,并将小数转换为向上/向下舍入的整数。因此,[10.2%]最终将成为10。而且,[11.8%]最终将是12 希望我已经提供了足够的信息。为什么不使用Regex 在本例中,我假设括号内的值始终是带小数的双精度值 string WithBrackets = "[11.8%]"; string AsDouble = Regex.Match(WithBrackets, "\d{1,9}\.\d{1,9}").valu

我想解析一个字符串,只返回括号符号之间的值,例如
[10.2%]
。然后我需要去掉
“%”
符号,并将小数转换为向上/向下舍入的整数。因此,
[10.2%]
最终将成为
10
。而且,
[11.8%]
最终将是12

希望我已经提供了足够的信息。

为什么不使用Regex

在本例中,我假设括号内的值始终是带小数的双精度值

string WithBrackets = "[11.8%]";
string AsDouble = Regex.Match(WithBrackets, "\d{1,9}\.\d{1,9}").value;
int Out = Math.Round(Convert.ToDouble(AsDouble.replace(".", ","));
使用正则表达式(Regex)在一个括号内查找所需的单词。 这是您需要的代码: 使用foreach循环删除%并转换为int

List<int> myValues = new List<int>();
foreach(string s in Regex.Match(MYTEXT, @"\[(?<tag>[^\]]*)\]")){
   s = s.TrimEnd('%');
   myValues.Add(Math.Round(Convert.ToDouble(s)));
}
List myValues=new List();
foreach(Regex.Match(MYTEXT,@“\[(?[^\]]*)\])”中的字符串s){
s=s.TrimEnd('%');
添加(Math.Round(Convert.ToDouble));
}

如果可以确保括号之间的内容为%,那么这个小函数将返回第一组括号之间的值。如果需要提取多个值,则需要对其进行一些修改

public decimal getProp(string str)
{
    int obIndex = str.IndexOf("["); // get the index of the open bracket
    int cbIndex = str.IndexOf("]"); // get the index of the close bracket
    decimal d = decimal.Parse(str.Substring(obIndex + 1, cbIndex - obIndex - 2)); // this extracts the numerical part and converts it to a decimal (assumes a % before the ])
    return Math.Round(d); // return the number rounded to the nearest integer
}

例如,
getProp(“我喜欢cookies[66.7%]”
提供了数字67

当然,有人可以为您提供足够的信息,但我们不是这样操作的。请发布您当前的代码,说明如何使用并解释哪些代码不起作用以及您被卡住的位置。Regex when
string.Split
可以吗?@dotTutorials,我如何忽略包含其他信息的括号,例如[info]?无论括号内的百分比是多少,我都需要如何修改它来解析字符串?@MattMcHone-我不明白。例如:?什么是不同种类的
%
?范围从[0.1%]到[100%]。@MattMcHone-OK。无需更改。我猜您不明白我发布的内容或它是如何工作的。@MattMcHone而不是
“[11.8%]”
放入字符串变量。
var s = "[10.2%]";
var numberString = s.Split(new char[] {'[',']','%'},StringSplitOptions.RemoveEmptyEntries).First();
var number = Math.Round(Covnert.ToDouble(numberString));
public decimal getProp(string str)
{
    int obIndex = str.IndexOf("["); // get the index of the open bracket
    int cbIndex = str.IndexOf("]"); // get the index of the close bracket
    decimal d = decimal.Parse(str.Substring(obIndex + 1, cbIndex - obIndex - 2)); // this extracts the numerical part and converts it to a decimal (assumes a % before the ])
    return Math.Round(d); // return the number rounded to the nearest integer
}