C# 提取字符串的最后一部分(节)

C# 提取字符串的最后一部分(节),c#,.net,extract,indexof,C#,.net,Extract,Indexof,我只需要提取/字符后字符串的最后一部分 我尝试了LastIndexOf,但失败了 有解决办法吗 企图 您可以省略第二个参数。这将调用重载,它返回一个子字符串,该子字符串从指定的字符位置开始,一直到字符串的末尾 string strDiv2 = tbxAff.Text.Substring(tbxAff.Text.IndexOf("/") + 1); 此外,如果要将提取的子字符串解析为双精度字符串,则可能需要排除分隔符。使用字符串.Split()函数: string[] y = tbxAff.T

我只需要提取
/
字符后字符串的最后一部分

我尝试了
LastIndexOf
,但失败了

有解决办法吗


企图
您可以省略第二个参数。这将调用重载,它返回一个子字符串,该子字符串从指定的字符位置开始,一直到字符串的末尾

string strDiv2 = tbxAff.Text.Substring(tbxAff.Text.IndexOf("/") + 1);
此外,如果要将提取的子字符串解析为双精度字符串,则可能需要排除分隔符。

使用
字符串.Split()
函数:

string[] y = tbxAff.Text.Split(new string[] { " / " }, StringSplitOptions.RemoveEmptyEntries);
然后像这样使用它:

string strDiv2 = y[1] // Second Part
dblDiv2 = Convert.ToDouble(strDiv2);

字符串clientSpnd=textBox1.Text.Substring(textBox1.Text.LastIndexOf(“”)+1)

这里有一种扩展方法,可以进行安全检查:

public static class StringExtensions
{
     public static string LastPartOfStringFrom(this string str, char delimiter )
     {
         if (string.IsNullOrWhiteSpace(str)) return string.Empty;

         var index = str.LastIndexOf(delimiter);

         return (index == -1) ? str : str.Substring(index + 1);
     }
}

欢迎来到Stackoverflow!请编辑您的问题并在此处插入C#代码。但是,这将对您有所帮助。将代码作为文本而不是图像发布。这将大大提高你得到答案的机会。此外,您可以从文本框中获取文本,因此您的问题实际上是“提取字符串的最后一部分”。您的问题与文本框无关,请从文本框中取出字符串,并使用字符串函数对其进行操作。您尝试过string.Split吗?LastIndexOf是一个不错的选择,让我们看看您尝试使用LastIndexOf时收到了什么错误消息?
public static class StringExtensions
{
     public static string LastPartOfStringFrom(this string str, char delimiter )
     {
         if (string.IsNullOrWhiteSpace(str)) return string.Empty;

         var index = str.LastIndexOf(delimiter);

         return (index == -1) ? str : str.Substring(index + 1);
     }
}