-6

この質問の続きとして

私が文字列を持っているとしましょう:

String myString = "violet are blue|roses are red|this is a terrible poet";

「バラは赤い|」と書かれている特定の部分をトリミングしたい 次のようになります。

myString = "violet are blue|this is a terrible poet";
4

4 に答える 4

3

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);
于 2012-12-15T18:53:51.630 に答える
2

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);
于 2012-12-15T18:55:15.070 に答える
1

分割の使用:

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];
于 2012-12-15T18:56:06.603 に答える
0

質問は正規表現の答えがなければ完全なものにはなりません:)

String myString = "violet are blue|roses are red|this is a terrible poet";
var newstr = Regex.Replace(myString, @"\|.+?\|", "|");
于 2012-12-15T19:45:24.140 に答える