Matching an integer between the brackets

前端 未结 5 831
有刺的猬
有刺的猬 2021-01-29 07:57

I am given bunch of strings in the following format:

ASDF [         6]

ZXC[1]

OtPasd[ 4 ]

asdffa[   7]

I need to retrieve the integer betwee

5条回答
  •  无人共我
    2021-01-29 08:24

    To get the int in between the brackets you can also try this way:

    string tmpString = "ASDF [         6]";
    int start = tmpString.IndexOf('[') + 1;
    int length = tmpString.IndexOf(']') - start;
    string subString = tmpString.Substring(start, length);
    int tempInt;
    if(Int.TryParse(subString, out tempInt))
    return tempInt;
    

提交回复
热议问题