(Integer readFrom: 'abc' readStream) = 0

Zulq Alam me at zulq.net
Wed Jan 9 11:24:35 UTC 2008


Thanks Damien.

That's almost what I need, except '123abc' = 123 and '1.1' = 1, etc. I 
think I want something more like this:

parseString: aString base: aBase ifFail: failBlock
	" Answer a new integer described by the whole string aString in
	base aBase. If the parse fails, return the value of the block
	failBlock."
	
	| stream integer |
	stream := aString readStream.
	integer := (SqNumberParser on: stream)
		nextIntegerBase: aBase
		ifFail: [^ failBlock value].
	stream atEnd ifFalse: [^ failBlock value].
	^ integer

parseString: aString ifFail: failBlock
	" Answer a new integer described by the whole string aString in
	base 10. If the parse fails, return the value of the block
	failBlock."
	
	^ self parseString: aString base: 10 ifFail: failBlock

Integer parseString: 'abc' ifFail: [#error] 	"#error"
Integer parseString: '123' ifFail: [#error] 	"123"
Integer parseString: '123abc' ifFail: [#error]. "#error"
Integer parseString: '1.1' ifFail: [#error].	"#error"
Integer parseString: '' ifFail: [#error] 	"#error"

Others might find this useful. Especially those looking for the 
equivalent to Integer.parseInt in Java. What would be a good way for 
making these types of changes available?

Thanks,
Zulq.

Damien Cassou wrote:
> 
> You should use #readExactlyFrom:
> 




More information about the Squeak-dev mailing list