C# 如何将字符串从“%”等字符修剪到C结尾

C# 如何将字符串从“%”等字符修剪到C结尾,c#,C#,如何将字符串从字符修剪到结尾?大概是这样的: 32.6% of 3.07MiB at 5.71MiB/s ETA 00:00 string str = "32.6% of 3.07MiB at 5.71MiB/s ETA 00:00 "; if (str.Contains('%')) { int index = str.IndexOf('%'); string result = st

如何将字符串从字符修剪到结尾?大概是这样的:

32.6% of 3.07MiB at  5.71MiB/s ETA 00:00 
string str = "32.6% of 3.07MiB at  5.71MiB/s ETA 00:00 ";
         if (str.Contains('%'))
            {
                int index = str.IndexOf('%');
                string result = str.Substring(0, index);
                Console.WriteLine(result);
            }
需要这样的输出

32.6

与使用Split的建议相比,我建议

value.Substring( 0, value.IndexOf( '%'))
。。。因为它不需要分配数组,而Split需要分配数组。

这样如何:

32.6% of 3.07MiB at  5.71MiB/s ETA 00:00 
string str = "32.6% of 3.07MiB at  5.71MiB/s ETA 00:00 ";
         if (str.Contains('%'))
            {
                int index = str.IndexOf('%');
                string result = str.Substring(0, index);
                Console.WriteLine(result);
            }

到目前为止你研究了什么?什么对你不起作用,你遇到了什么错误,你不明白什么?试过string.Trim但效果不好,bcs编号不同,例如0、32、100等。如果你阅读文档,string.Trim会做完全不同的事情。你读过关于string.Substring和string.IndexOf的文档了吗?那不是修剪。字符串结果=3.07MiB在5.71MiB/s ETA 00:00时的32.6%。拆分“%”[0];需要检查索引是否小于0。