Picking up content within a string

Asked

Viewed 1,303 times

4

I’ll have a list of string in the following format: PERSON’S NAME( (LOGIN), an example would be this: ZACKSON MOREIRA MORGAN (zackson.morgan)

Soon, I would need to get just what would be inside the parentheses, ie, zackson.Morgan.

How to do this?

  • Detail: all strings in the list will always follow this format.

2 answers

8


A simple way to do this is to locate the position of the two parentheses and take the inner part, like this:

using static System.Console;
                    
public class Program {
    public static void Main() {
        var texto = "ZACKSON MOREIRA MORGAN (zackson.morgan)";
        WriteLine(pegaTexto(texto));
        texto = "ZACKSON MOREIRA MORGAN (zackson.morgan";
        WriteLine(pegaTexto(texto));
        texto = "ZACKSON MOREIRA MORGAN (";
        WriteLine(pegaTexto(texto));
    }
    public static string pegaTexto(string texto) {
        texto += ")";
        texto = texto.Substring(texto.IndexOf("(") + 1);
        return texto.Substring(0, texto.IndexOf(")"));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

With the advent of Span it is possible to do this more optimally.

  • Much better than the people’s regex mania!

  • Perfect, I did not know this site. I’m still new in programming . net, and I’m really enjoying it. I want to delve into it. Thanks for the help!

  • @Zackmorgan welcome then. Do not forget to take a look at the [tour] to understand the basics. Here the staff likes to do everything with quality.

  • @bigown It’s really necessary to put a ) at the end of the string before taking the desired text?

  • 1

    @mutlei is, because will they forgot to close. There also works.

  • @bigown Another question, why there is a Exception when you try to do operation on a single line, with the contents of the (+1 and of )?

  • I don’t know, I’d have to see the code.

  • I searched the documentation of the method and saw only a foolish reason for launching this exception... He makes exception in these cases: startIndex mais length não indica uma posição dentro dessa instância. or startIndex ou length é menor que zero. The second I understood, but the first... WTF.

  • @mutlei do not know how it is testing but I made a change and more tests.

  • I tried with texto.Substring(texto.IndexOf("(") + 1, texto.IndexOf(")")), that would work in Java. But as I read in the documentation, it generates an Exception ArgumentOutOfRangeException why the sum of the indices is greater than the length of the string.

  • Ah, you’re doing something else. It’s dangerous to do this because then you run the risk of having a parenthesis closing before you have an opening. And there are other problems in this code. I gave a solution that always works. It might not even be ideal, it might report a problem if you don’t have a parenthesis closing. But I would not report this except: http://answall.com/q/21767/101

  • That’s the language itself. Link

Show 7 more comments

1

While the @Maniero solution largely solves the issue, the version is left with regular expressions to solve the problem:

private static Regex regex = new Regex("^.*\\((?<login>.*)\\)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);

public static void Main()
{
    WriteLine(GetLogin("ZACKSON MOREIRA MORGAN (zackson.morgan)"));
    WriteLine(GetLogin("ZACKSON MOREIRA MORGAN (zackson.morgan"));
    WriteLine(GetLogin("ZACKSON MOREIRA MORGAN )"));
    WriteLine(GetLogin("ZACKSON MOREIRA MORGAN ("));
    WriteLine(GetLogin("ZACKSON MOREIRA MORGAN"));
}

private static string GetLogin(string input)
{               
    var res = regex.Match(input);
    if(res.Success)
        return res.Groups["login"].Value;
    else
        return "No matches found";
}

In this case, the syntax (?<input>.*) captures all characters between parentheses (defined by \\( and \\)).

See working on dotNetFiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.