strspn?

  • Thread starter Thread starter Nils Gösche
  • Start date Start date
N

Nils Gösche

Hi!

Quick question:

Does the standard C function strspn have an equivalent in the .net
standard library?

Regards,
 
Nils Gösche brought next idea :
Hi!

Quick question:

Does the standard C function strspn have an equivalent in the .net
standard library?

Regards,

I think you need to dive into the Regex object
(System.Text.RegularExpressions).

this C code:
int i;
char strtext[] = "129th";
char cset[] = "1234567890";

i = strspn (strtext,cset);


would be something like:
// using System.Text.RegularExpressions;
int len = 0;
Regex reNum = new Regex("[0-9]+");
Match m = reNum.Match("129th");
if (m.Success)
len = m.Length;


Hans Kesting
 
Hans Kesting said:
Nils Gösche brought next idea :
I think you need to dive into the Regex object
(System.Text.RegularExpressions).

this C code:
int i;
char strtext[] = "129th";
char cset[] = "1234567890";

i = strspn (strtext,cset);


would be something like:
// using System.Text.RegularExpressions;
int len = 0;
Regex reNum = new Regex("[0-9]+");
Match m = reNum.Match("129th");
if (m.Success)
len = m.Length;

Yeah, I already suspected I'd have to do it this way. The reason I
didn't like this version is because my 'cset' is not fixed but comes
from user input, so I'll have to escape things like \ or ^ or - which
seems messy and error-prone. I guess I'll just write a trivial
implementation of strspn that checks character after character.

Thanks for your reply, anyway!
 
Does the standard C function strspn have an equivalent in the .net
standard library?

Not as such, but it's trivially done with LINQ as a one-liner:

string str, charsToSkip;
str.Where(ch => charsToSkip.IndexOf(ch) < 0).Select((ch, i) =>
i).First()
 
Pavel Minaev said:
Not as such, but it's trivially done with LINQ as a one-liner:

string str, charsToSkip;
str.Where(ch => charsToSkip.IndexOf(ch) < 0).Select((ch, i) =>
i).First()

Nice :-p

Regards,
 
Back
Top