[英]what is the best way to parse out the text after the last instance of a character
我有以下字符串:
string test = /test/test1/tse3/ttese3/test3-45-NameToParseOut
我需要解析出“ NameToParseOut”一詞。 我基本上需要找到最后一個“-”,並在“-”的最后一個實例之后返回所有文本。 用C#解析此問題的最優雅的方法是什么?
string test = "/test/test1/tse3/ttese3/test3-45-NameToParseOut";
test = test.Substring(test.LastIndexOf('-')+1);
即使對於不包含破折號的字符串也可以使用(在這種情況下,將返回整個字符串)。
嘗試:
string test = "/test/test1/tse3/ttese3/test3-45-NameToParseOut";
int index = test.LastIndexOf('-');
string value;
if(index != -1) {
value = test.Substring(index) + 1;
}
在此處閱讀有關LastIndexOf(char)的更多信息: http : //msdn.microsoft.com/zh-cn/library/aa904293%28v=VS.71%29.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.