SilentSin
SilentSin

Reputation: 1036

How can I float.TryParse on a substring of a whole line?

I'm making a text serialization system where each line contains memberName value. I can identify the member without allocating a substring by finding the char index of the space and using string.Compare(line, 0, memberName, 0, indexOfSpace), but when I go to parse the rest of the line to get the value it seems that I need to create a substring to pass into float.TryParse (and the similar methods for other primitive types).

Are there methods like TryParse in some other class which take substring indices? Otherwise, is there somewhere I can see the source code of those methods to implement my own variations?

Upvotes: 2

Views: 177

Answers (1)

Heinzi
Heinzi

Reputation: 172390

Are there methods like TryParse in some other class which take substring indices?

No.

Otherwise, is there somewhere I can see the source code of those methods to implement my own variations?

Yes, Double.TryParse is available in the Microsoft Reference Source repository:


As a general word of advice: In most cases, creating a substring is very unlikely to be relevant with respect to performance or memory usage. Be sure that this is really the bottleneck of your application before investing a lot of effort to micro-optimize here.

Upvotes: 3

Related Questions