-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #1937 string iterator suffix #1938
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,56 @@ of existing strings. For example:: | |
SET s TO "Hello, Strings!". | ||
SET t TO s:REPLACE("Hello", "Goodbye"). | ||
|
||
Strings are iterable. This scripts prints the string's characters one per line:: | ||
ACCESSING INDIVIDUAL CHARACTERS | ||
------------------------------- | ||
|
||
There's two main ways to access the individual characters | ||
of a string - using an iterator or using index numbers: | ||
|
||
Using an Iterator (FOR) | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Strings can be treated a little bit like iterable lists | ||
of characters. This allows them to be used in FOR loops | ||
as in the example below: | ||
|
||
SET str TO "abcde". | ||
LOCAL str is "abcde". | ||
|
||
FOR c IN str { | ||
PRINT c. | ||
PRINT c. // prints "a" the first time, then "b", etc. | ||
} | ||
|
||
The reason you can use Strings with the FOR loop like this is | ||
because you can obatain an :struct:`Iterator` of a string with the | ||
:attr:`ITERATOR` suffix mentioned below. (Any type that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I'll be pointing to |
||
implements the ITERATOR suffix can do this.) | ||
|
||
Using an Index ( [i] ) | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Strings can also be treated a little bit like lists in that | ||
they allow you to use the square-brackets operator `[`..`]` | ||
to choose one character by its index number (numbers start | ||
counting at zero). Here's an example that does the same thing | ||
as the FOR loop above, but using index notation:: | ||
|
||
LOCAL str is "abcde". | ||
local index is 0. | ||
until index = str:LENGTH { | ||
print str[index]. | ||
set index to index + 1. | ||
} | ||
|
||
Be aware that despite being able to read the characters this way, | ||
you cannot set them this way. The following will give | ||
an error:: | ||
|
||
LOCAL str is "abcde". | ||
|
||
// The following line gives an error because you can't | ||
// change the characters inside a string: | ||
set str[0] to "X". | ||
|
||
Boolean Operators | ||
----------------- | ||
|
||
|
@@ -127,6 +169,9 @@ Structure | |
* - :meth:`INSERT(index, string)` | ||
- :struct:`String` | ||
- Returns a new string with the given string inserted at the given index into this string | ||
* - :attr:`ITERATOR` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all of our other structures, we don't duplicate suffixes from inherited structure types. But this table doesn't direct the user to go look for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does string inherit from Enumerator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh hey, you're right. It is literally just an iterator suffix, not the rest of the enumerator stuff... I take it all back. I'll just remove the enumerator portion of that attribute definition below instead. Ignore my craziness. |
||
- :struct:`Iterator` | ||
- generates an iterator object the elements | ||
* - :meth:`LASTINDEXOF(string)` | ||
- :struct:`Scalar` | ||
- Alias for FINDLAST(string) | ||
|
@@ -233,6 +278,18 @@ Structure | |
|
||
Returns a new string with the given string inserted at the given index into this string | ||
|
||
.. attribute:: Enumerable:ITERATOR | ||
|
||
:type: :struct:`Iterator` | ||
:access: Get only | ||
|
||
An alternate means of iterating over a string's characters | ||
(See: :struct:`Iterator`). | ||
|
||
For most programs you won't have to use this directly. It's just | ||
what enables you to use a string with a FOR loop to get access | ||
to its characters one at a time. | ||
|
||
.. method:: String:LASTINDEXOF(string) | ||
|
||
Alias for FINDLAST(string) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add a
:
for the code example to work. I'll handle it in my review edits.