strspn?

N

Nils Gösche

Hi!

Quick question:

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

Regards,
 
H

Hans Kesting

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
 
N

Nils Gösche

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!
 
P

Pavel Minaev

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()
 
N

Nils Gösche

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top