如何在括号之间包含文本[重复]

时间:2021-07-07 21:41:56

This question already has an answer here:

这个问题在这里已有答案:

I have 3 different string values in my console application:

我的控制台应用程序中有3个不同的字符串值:

Student A (AA.1)

学生A(AA.1)

Student(1) B (AA.2)

学生(1)B(AA.2)

Student B

学生B.

Would like to get value from backet:

想从backet获得价值:

AA.1, AA.2

AA.1,AA.2

Thanks. I have used split, however will encounter error because of Student B does not have any bracket and Student(1) B (AA.2) have 2 brackets.

谢谢。我曾经使用过分裂,但是会因为学生B没有任何支架而遇到错误而且学生(1)B(AA.2)有2个括号。

Current Code

现行守则

 char[] targetExpression = new char[] { '(', ')' };
 string title = "Student A (AA.1)";
 Console.WriteLine(title.Split(targetExpression)[1]);

2 个解决方案

#1


1  

looking for something like this?

寻找这样的东西?

/.*\(([^)]*)\)$/

#2


0  

A function to get a string within characters

在字符中获取字符串的函数

string GetStirngWithiin(string SourceString, char startChar, char endChar,int StartFrom=0)
{
    SourceString = SourceString.Substring(StartFrom);
    int startFrom=SourceString.IndexOf(startChar);
    int EndAt=SourceString.IndexOf(endChar);
    if ((startFrom > -1) && (EndAt >-1))
    {
       int SubStrLength = EndAt - startFrom - 1;
        if (SubStrLength > 0)
        {
          string Result = SourceString.Substring(startFrom + 1, SubStrLength);
          return Result;
         }

      }
   return "";
}

you can use the function calls as

你可以使用函数调用

 string str1 = GetStirngWithiin("Student A (AA.1)",'(',')');
 string str2 = GetStirngWithiin("Student(1) B (AA.2))", '(', ')',10);

using with Student B will return a blank string.

与学生B一起使用将返回一个空白字符串。

#1


1  

looking for something like this?

寻找这样的东西?

/.*\(([^)]*)\)$/

#2


0  

A function to get a string within characters

在字符中获取字符串的函数

string GetStirngWithiin(string SourceString, char startChar, char endChar,int StartFrom=0)
{
    SourceString = SourceString.Substring(StartFrom);
    int startFrom=SourceString.IndexOf(startChar);
    int EndAt=SourceString.IndexOf(endChar);
    if ((startFrom > -1) && (EndAt >-1))
    {
       int SubStrLength = EndAt - startFrom - 1;
        if (SubStrLength > 0)
        {
          string Result = SourceString.Substring(startFrom + 1, SubStrLength);
          return Result;
         }

      }
   return "";
}

you can use the function calls as

你可以使用函数调用

 string str1 = GetStirngWithiin("Student A (AA.1)",'(',')');
 string str2 = GetStirngWithiin("Student(1) B (AA.2))", '(', ')',10);

using with Student B will return a blank string.

与学生B一起使用将返回一个空白字符串。