【问题标题】:C# remove string inside bounderiesC# 删除边界内的字符串
【发布时间】:2020-09-21 08:28:24
【问题描述】:

对不起,如果这是重复的或什么,但我无法在 c# 中找到解决我的问题的方法。我有 c#string,看起来像这样:

string aa = "First Example (first to delete) , Second example ( Second to delete ) , Thrird Example ( Third to delete )"

我想从这个字符串中删除( SOME INPUT ) 中的所有内容,其中包含()

问题是我不知道这个SOME INPUT 是什么。我只知道在()里面。

感谢您的帮助。

【问题讨论】:

标签: c# string c#-4.0 string-matching


【解决方案1】:

https://dotnetfiddle.net/Jnhszs

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
    var text = "First Example (first to delete) , Second example ( Second to delete ) , Thrird Example ( Third to delete )";

    text = Regex.Replace(text, @"\([^)]*\)", "");
    text = Regex.Replace(text, @"\s{2,}", " ");

    Console.WriteLine(text);
    }
}

输出:第一个例子,第二个例子,第三个例子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多