[英]how do i delete one part of a string?
String mystring="start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;";
建议我现在想获取“ importante”的值,因为我知道如何使用子字符串,但是它有2个子项,那么我该如何获取,第一个,第二个之后? ... ?? 如果不可行,我想尝试一下...保存第一个。 并从“开始”开始删除,直到下一个查询的value5保存value2 ...如何做两件事中的任何一个?
我得到了第一个价值,所以...
string word = "important=";
int c= mystring.IndexOf(word);
int c2 = word.Length;
for (int i = c+c2; i < mystring.Length; i++)
{
if (mystring[i].ToString() == ";")
{
break;
}
else
{
label1.Text += mystring[i].ToString(); // c#
// label1.setText(label1.getText()+mystring[i].ToString(); //java
}
}
如果要提取所有值,则可以使用正则表达式:
string input = "start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;";
Regex regex = new Regex(@"important=(?<value>\w+)");
List<string> values = new List<string>();
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
string value= match.Groups["value"].Value;
values.Add(value);
}
您可以将值保存在数组中,而不是通过MessageBox显示它们。
string mystring = "start i dont know hot text can it to have here important=value5; x=1; important=value2; z=3;";
string temp = mystring;
string word = "important=";
while (temp.IndexOf(word) > 0)
{
MessageBox.Show( temp.Substring(temp.IndexOf(word) + word.Length).Split(';')[0]);
temp = temp.Remove(temp.IndexOf(word), word.Length);
}
使用正则表达式,找到所有匹配项并自己重建字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.