他最后一个特定的字符串我怎么切? [英] How can I cut any string when he het last specific string?

查看:112
本文介绍了他最后一个特定的字符串我怎么切?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像
这样的字符串

I have a string like

string str=" Motin AS fahami.menu.Asgar AS ASRAF"



我一直想得到字符串befire最后一次出现的AS.该AS始终被视为单个AS.此"AS"将不考虑ASRAF部分,其中前两个单词也表示AS.

输出与"Motin AS fahami.menu.Asgar"相同.

如果代码是这样的



I always want to get the string befire last occurring AS. This AS is always consider as single AS. This ''AS'' will not consider the portion ASRAF where first two word also indicate AS.

The out will be same as ''Motin AS fahami.menu.Asgar''.

if the code is like that

string str=" Motin AS fahami.menu.ASIM AS SUVASISH"



那么输出结果将是:Motin AS fahami.menu.ASIM

有人可以给我提供该代码吗?



then the out put will : Motin AS fahami.menu.ASIM

can anyone provide me the code for that?

推荐答案

请参阅以下内容:
string str=" Motin AS fahami.menu.ASIM AS SUVASISH";
string str2;
int i = str.LastIndexOf(" AS ");
str2 = str.Substring(0, i);


一种相当笨拙的方法是:

A fairly clunky way would be:

string delimiter = " AS ";  //note the spaces either side
string str = "Motin AS fahami.menu.ASIM AS anything that doesn't contain the delimiter";
string[] elements = System.Text.RegularExpressions.Regex.Split(str, delimiter);
string result = elements[0] + delimiter + elements[1];


如果我确实了解您的需要,
然后这就是您需要的:

Hi, if i did understood right your need,
Then here is what you need:

string str=" Motin AS fahami.menu.ASIM AS SUVASISH"

if(str.Contains(" AS ")) //if you use indexOf in a string without being sure the string conatins your search string, an exception will be thrown
{               
                //gett the index of the last occurance
                int idexOfLastOccurance = str.LastIndexOf(" AS ");
                //Remove all the text starting from the index      
                str = str.Remove(idexOfLastOccurance);
}


这篇关于他最后一个特定的字符串我怎么切?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆