开发者

stripping out unknown values in C#

开发者 https://www.devze.com 2023-03-29 18:46 出处:网络
I currently have the following code to get a specific value i need from a string. char[] delimiterChars = { \' \', \'B\', \'O\', \'B\'};

I currently have the following code to get a specific value i need from a string.

char[] delimiterChars = { ' ', 'B', 'O', 'B'};
                  string text = input.ToString();
                  string[] words = text.Split(delimiterChars);

Unfortunately before the space in the string is unknown characters that are entered in by the user. Is there a wild card that gets all the characters before that space? I was hoping that i could just put a * inside of the first quotation with the space '* ' but it doesn't seem to like that.

Thanks.

EDIT:

So the string variable is made up of a series of characters inputted by the user then it has BOB and a random integer added t开发者_运维技巧o the end. My goal is to retrieve just the number at the end of the string.

An example of a sample string would be "user BOB44" Sorry for the confusion and thanks again!


You can use a regular expression to get the number at the end of a string

/([0-9]*)$/ will capture the last group of sequential numeric characters at the end of a string

In C# (it looks like you're using that language):

Regex pattern = new Regex("([0-9]*)$");
MatchCollection matches = pattern.Matches(input);
if (matches.Count > 0) {
    // matches[0] will contain the number
}
0

精彩评论

暂无评论...
验证码 换一张
取 消