[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-smalltalk] Best way to find if a string begins with a particul
From: |
Rick Flower |
Subject: |
Re: [Help-smalltalk] Best way to find if a string begins with a particular string.. |
Date: |
Wed, 17 Oct 2012 08:43:08 -0700 |
User-agent: |
Apple Webmail/0.6 |
By the way.. I added a few methods that I found very helpful in my text
processing I've been doing lately.. Perhaps someone else might find
these
of interest -- they're modeled after similar methods already present
but
these work for stings instead of single characters. Enjoy!
Stream extend [
skipToString: aString [
<comment: 'Skip to the specified string in the file -- must match the
entire line!!'>
[self atEnd] whileFalse: [
self nextLine = aString ifTrue: [^true]].
^false
]
findSubstring: aString [
<comment: 'Skip to the specified sub-string in the stream'>
| string |
[self atEnd] whileFalse: [
string := self nextLine.
(string includesSubstring: aString caseSensitive: false)
ifTrue: [
"match found.. back the stream up to the start of
the line"
self position: (self position - string size -
1).
^true
]
].
^false
]
upToString: aString [
| nextLine ws |
ws := WriteStream on: (self species new: 8).
[self atEnd or: [(nextLine := self nextLine) = aString]]
whileFalse: [ws nextPutAll: nextLine. ws nextPut:
Character nl].
^ws contents
]
upUntilStringPrefixMissing: aString [
| nextLine ws |
ws := WriteStream on: (self species new: 8).
[self atEnd or: [(nextLine := self nextLine) startsWith:
aString]]
whileTrue: [ws nextPutAll: nextLine. ws nextPut:
Character nl].
^ws contents
]
]
Re: [Help-smalltalk] Best way to find if a string begins with a particular string.., Gwenaƫl Casaccio, 2012/10/17