Getting words between other words

Hi all

I’m trying to obtain the second number from the below:

14 to do(s) in 14 file(s)

Both of the numbers could change from double to single figures. I’ve tried Variable.Substring(5) however that leaves me with “14 file(s)” when I only want the 14.

Could someone let me know what I’m missing?

Thanks!

Hi.

There are many options to this. Is the word count consistent? If it is, you could simply do
variable.Split(" "(0))(4)
to pull the 5th word.

edited for typo

Hey,

It’s saying "Option Strict On disallows implicit conversions from ‘String’ to ‘Char’.

Do I need to change the variable type from String to something else?

Sorry had a typo.
variable.Split(" "(0))(4)

" " is supposed to be a character so the (0) converts it to a character. I was missing a ‘)’ after it.

Thanks.

Use the c type character instead:
variable.Split(" "c)(4)
It’s clearer in intent, reading and harder to make an error with it.

1 Like

Thanks both, got it working :slight_smile:

Is there a specific website you learnt all this from? I Googled before I asked and couldn’t find anything remotely like the above solutions, so I’d rather learn than ask

Thanks again!

@andrzej.kniola thanks, good to know

@Short
Much of it is vb.net or C# syntax, so you can include that in your searches.

You can also rearrange it to say Split(variable," "c)(4) so just depends on your preference.

I’m not an expert though, but just have the most used ones memorized for string manipulation

Well split is a good way and fast too although have not done benchmark :stuck_out_tongue: but one more way to do is to use LINQ.

Get all numbers only from string and convert it into array then access the second index.

String text = “14 to do(s) in 14 file(s)”

Int numbers = (from m in Regex.Matches(text,“\d+”) select Int32.Parse(m.ToString)).ToArray()

Writeline → numbers(1);

Note - you have to import following namespaces :slight_smile: System.Linq and System.Text.RegularExpressions

Regards…!!
Aksh

2 Likes