Skip to content

Find the pattern “[Number1, Number2]” in any given String and get the Integer (32bit) values for Number1 and Number2

murali krishna

Asked Mar 30, 2007, 4:06 AM EDT1 answer
Share
LinkedInX
how can I do the following task if any one can help with this.

I need to find the pattern from the following string and get the int32 bit values from number1,number2.

Example Input:“afsad fsdaf [25,33] fasdffa fasfasfa”

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Here is one way to do it:

    int firstNumber;
    int secondNumber;
    string myString = "afsad fsdaf [25,33] fasdffa fasfasfa";
    string strNumbers = myString.Substring(myString.IndexOf('[') + 1, myString.IndexOf(']') -myString.IndexOf('['));
    char[] commaSep ={ ',' };
    string[] numbers = strNumbers.Split(commaSep);
    if (numbers.Length > 0)
       firstNumber = int.Parse(numbers[0]);
    if (numbers.Length > 1)
       secondNumber = int.Parse(numbers[1]);


    I've written a function called GetStringInBetween that can do the above for you, you can check this articles http://www.mycsharpcorner.com/Post.aspx?postID=15 for the source code.

    You can also use Regular expressions, which are much compact.

    Hope this helps
    0