この質問の続きとして
私が文字列を持っているとしましょう:
String myString = "violet are blue|roses are red|this is a terrible poet";
「バラは赤い|」と書かれている特定の部分をトリミングしたい 次のようになります。
myString = "violet are blue|this is a terrible poet";
You can use String.Replace
method
string input = "violet are blue|roses are red|this is a terrible poet";
string expected = "violet are blue|this is a terrible poet";
string actual = input.Replace("roses are red|", String.Empty);
Debug.Assert(expected == actual);
I assume that it's the |
characters that you want to use to find what to remove.
Get the index of the first |
using IndexOf
, then the second, then get the remaining parts of the string using Remove
:
int index1 = myString.IndexOf('|');
int index2 = myString.IndexOf('|', index1 + 1);
myString = myString.Remove(index1, index2 - index1);
分割の使用:
const string myString = "violet are blue|roses are red|this is a terrible poet";
const char itemToSplitOn = '|';
var arr = myString.Split(itemToSplitOn);
var newString = arr[0] + itemToSplitOn + arr[2];
質問は正規表現の答えがなければ完全なものにはなりません:)
String myString = "violet are blue|roses are red|this is a terrible poet";
var newstr = Regex.Replace(myString, @"\|.+?\|", "|");