如何在某个字符最后一次出现后删除所有文本 [英] how to remove all text after the last recurrence of a certain character
本文介绍了如何在某个字符最后一次出现后删除所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定任何字符串,我想删除特定字符后的任何字母.
given any string, i want to remove any letters after a specific character.
这个字符可能在字符串中多次出现,我只想把它应用到最后一次出现.
this character may exist multiple times in the string and i only want to apply this to the last occurrance.
所以让我们说/"是字符,这里有一些例子:
so lets say "/" is the character, here are some examples:
http://www.ibm.com/test ==> http://www.ibm.com
你好/测试 ==> 你好
http://www.ibm.com/test ==> http://www.ibm.com
hello/test ==> hello
推荐答案
if (text.Contains('/'))
text = text.Substring(0, text.LastIndexOf('/'));
或
var pos = text.LastIndexOf('/');
if (pos >= 0)
text = text.Substring(0, pos);
(编辑以涵盖字符串中不存在/"的情况,如注释中所述)
(edited to cover the case when '/' does not exist in the string, as mentioned in comments)
这篇关于如何在某个字符最后一次出现后删除所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文