Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从字符串中删除字符序列_C#_String - Fatal编程技术网

C# 如何从字符串中删除字符序列

C# 如何从字符串中删除字符序列,c#,string,C#,String,我想从给定的字符串a中删除未知数量的字符序列B。 删除必须从字符序列C位置的右侧开始。删除必须在B字符序列结束时停止 字符串A的示例: xxxxxxxxBxxxxxxxxxxxxxxxxxCBBBBBByyyyyyyyyByyyy A ... sequence of characters from which B's that follow C must be removed C ... a sequence of characters (example: 123) B ... a seque

我想从给定的字符串a中删除未知数量的字符序列B。 删除必须从字符序列C位置的右侧开始。删除必须在B字符序列结束时停止

字符串A的示例:

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
在此示例中,必须删除C之后的所有B。不得移除所有其他B

结果将是:

xxxxxxxxBxxxxxxxxxxxxxxxxxCyyyyyyyyyByyyy
我尝试使用:

A = A.replace("vbz","");
但这会从A中删除每个“vbz”序列。 我如何排除删除那些不在C前面的“vbz”


你好,马努,你为什么不试试这个

var.Replace("x", "");
var.Replace("y", "");
只需将
x
y
替换为未知的字符串序列

string A=“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXCBBBBYYYYYYYYYYYYYYYYYYYYYYY”;
string A = "xxxxxxxxBxxxxxxxxxxxxxxxxxCBBBBBByyyyyyyyyByyyy";
string pattern = @"(?<=C)[B]*";
string B = Regex.Replace(A, pattern, "");
字符串模式=@”(?
根据您的要求,从字符串中删除需要满足两个条件:
1.未知数量的字符串序列B
2.拆卸必须从字符串C位置的右侧开始
可以使用System.Text.RegularExpressions命名空间的正则表达式类来实现。
字符串A=“XXXXXXXX BXXXXXXXXXXXXXXXXXXXXXXXXCBBBBBBYYYYYYYYYYYYYYYY”;

string pattern=“(?代码段可能错误地表明
Replace
是一个静态方法(
string
是一个保留字)字符串类有很多辅助方法,比如IndexOf和Split,LastIndexOf。首先将字符串拆分成更小的部分,然后将这些部分传递给下一个函数,该函数找到终止序列,并在拆分后将其传递给下一个函数,重复直到完成k进入林克。
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