2017-06-10 38 views
-2

指定された文字列Aから未知数の文字列Bを削除したいA. 文字列Cの位置より右に削除を開始する必要があります。 B文字シーケンスが終了する。文字列Aの文字列から文字列を削除する方法

例:C結局Bさんは、除去しなければならない。この例では

xxxxxxxxBxxxxxxxxxxxxxxxxxCBBBBBByyyyyyyyyByyyy 

A ... sequence of characters from which B's that follow C must be removed 
C ... a sequence of characters (example: 123) 
B ... a sequence of characters (example: vbz) 
x and y ... any characters 

。その他のBはすべて削除しないでください。

結果は次のようになります。

xxxxxxxxBxxxxxxxxxxxxxxxxxCyyyyyyyyyByyyy 

私が使用してみました:

A = A.replace("vbz",""); 

が、それは私がそれらの「VBZの除去を除外することができますどのようにA. からすべての 'VBZ' 配列を削除しますCで先行していないものは?

よろしく、マヌ

+0

不明な文字列配列とxyを交換してください。最初に文字列を小さな部分に分割し、その部分を次の関数に渡して終了シーケンスを見つけ出し、分割した後に次の関数に渡し、完了するまで繰り返します。また、LINQを調べます。 –

答えて

1

は、なぜあなたはこれを試してみませんか?

var.Replace("x", ""); 
var.Replace("y", ""); 

だけで文字列クラスは、IndexOfメソッドやスプリット、のlastIndexOfとしてヘルパーメソッドをたくさん持っている

+2

スニペットは、 'Replace'が静的メソッド(' string'は予約語)であり、それが呼び出された文字列を変更することを間違って示唆するかもしれません。 –

1
string A = "xxxxxxxxBxxxxxxxxxxxxxxxxxCBBBBBByyyyyyyyyByyyy"; 
string pattern = @"(?<=C)[B]*"; 
string B = Regex.Replace(A, pattern, ""); 
0
As per your requirement, 2 conditions need to be satisfied for removing from a string : 

1. unknown number of string sequences B 
2. The removing must start to the right of the position of a string C 

It can be achieved using Regex class of System.Text.RegularExpressions namespace. 

string A = "xxxxxxxxBxxxxxxxxxxxxxxxxxCBBBBBByyyyyyyyyByyyy"; 
string pattern = "(?<=C)[b]*"; 
string result = Regex.Replace(A, pattern,"",RegexOptions.IgnoreCase); 


Note : 
pattern variable contains regex pattern. 
(cb*) : 
() : defines group of characters 
c : starting string 
b : B or b ; i.e, need to be replaced or removed 
* : defines multiple number of characters defines before * 
(?<=c) : Match any position following a prefix "c" 
RegexOptions.IgnoreCase : it says the removed character can be any case like B or b 
関連する問題