回答
您可以使用IndexOf
method和Substring
method像這樣:
string output = input.Substring(input.IndexOf('.') + 1);
上面沒有錯誤處理,所以如果輸入字符串中不存在句點,則會出現問題。
@casperOne實際上,如果沒有找到,實際上IndexOf不返回-1,並且字符串用於引用子字符串,所以在技術上如果沒有句點它會返回整個字符串,對嗎?我認爲只要字符串不爲空就足夠了。 – jcolebrand 2010-06-02 16:00:34
@drachenstern:如果未找到,IndexOf將返回-1,如果傳遞給Substring,則將引發ArgumentOutOfRangeException。 – casperOne 2010-06-02 16:25:48
@casperOne〜tut tut,它的-1'+ 1' ...所以它會一直是零或更多。你的代碼不是我的;) – jcolebrand 2010-06-02 16:31:43
String input = ....;
int index = input.IndexOf('.');
if(index >= 0)
{
return input.SubString(index + 1);
}
string input = "America.USA"
string output = input.Substring(input.IndexOf('.') + 1);
public string RemoveCharactersBeforeDot(string s)
{
string splitted=s.Split('.');
return splitted[splitted.Length-1]
}
注意:這將導致保留虛線的兩個(或全部)部分,如果您需要所有這些部分,那麼這些部分是可取的,但如果您只需要最後一部分則不需要。 – Joey 2010-06-02 15:57:01
你可以試試這個:
string input = "lala.bla";
output = input.Split('.').Last();
如果有多個期間,您將遇到問題。 – 2013-02-27 16:44:53
@MikeCole爲什麼? .Last()將確保您在一段時間後得到最後一個單詞 – 2015-11-30 03:51:22
兩個方法,如果字符不存在,返回原始的字符串。
這一個切串樞軸的第一次出現之後:
public static string truncateStringAfterChar(string input, char pivot){
int index = input.IndexOf(pivot);
if(index >= 0) {
return input.Substring(index + 1);
}
return input;
}
這一個,而不是削減樞軸的最後出現之後的字符串:
public static string truncateStringAfterLastChar(string input, char pivot){
return input.Split(pivot).Last();
}
- 1. 刪除前導字符
- 2. 刪除字母之前的字符串
- 3. 按字符刪除字符
- 4. 刪除字符
- 5. 刪除名稱前的字符串中的所有字符
- 6. 刪除字符串中的逗號前的字符
- 7. MySQL - 刪除給定字符串或字符前後的空格
- 8. 如何刪除指定字符串前的2個字符
- 9. 刪除每個字符串的前幾個字符
- 10. 刪除字符串中的前3個字符
- 11. 從python中的字符串中刪除一組前綴字符
- 12. 如何刪除字符串中的前10個字符?
- 13. 刪除破折號前的字符
- 14. 刪除以前的字符串條目
- 15. Python - 刪除字符串的前兩行
- 16. AppleScript - 刪除字符串的前10行
- 17. 刪除Python中字符串中特定子字符串前後的字符
- 18. JS - 刪除字符串前後的所有字符(並保留該字符串)?
- 19. 刪除字符數組中的字符
- 20. 刪除字符串中的字符串
- 21. 刪除c字符串中的字符
- 22. 刪除字符串中的字符
- 23. 刪除字符串後的字符?
- 24. 從SQL字符串的字符刪除
- 25. 刪除字符串中的 字符java
- 26. 刪除字符串中的字符
- 27. 刪除字符數組的字符
- 28. 刪除字符串中的字符
- 29. 如何刪除連字符「 - 」但計數之前刪除連字符在C#中?
- 30. 從字符串字符中刪除前綴u's
有沒有永遠只打算是一個時期? – 2010-06-02 15:54:05
@James Black:是 – loviji 2010-06-02 15:55:09