String API Functions: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) |
Cg (Diskussion | Beiträge) |
||
Zeile 122: | Zeile 122: | ||
<br>''aString''<code>.copyReplaceString_withString(</code> ''oldString'', ''newString'' <code>)</code> [JS] |
<br>''aString''<code>.copyReplaceString_withString(</code> ''oldString'', ''newString'' <code>)</code> [JS] |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello small world' copyReplaceString:'small' withString:'big' |
'hello small world' copyReplaceString:'small' withString:'big' |
||
=> ' hello big world ' |
=> ' hello big world ' |
||
Zeile 127: | Zeile 128: | ||
'hello small world' copyReplaceString:'big' withString:'bigger' |
'hello small world' copyReplaceString:'big' withString:'bigger' |
||
=> ' hello small world ' |
=> ' hello small world ' |
||
</div> |
|||
''aString'' <code>withoutPrefix:</code>''prefixString'' [ <code>caseSensitive:</code> ''boolean'' ] |
''aString'' <code>withoutPrefix:</code>''prefixString'' [ <code>caseSensitive:</code> ''boolean'' ] |
||
Zeile 133: | Zeile 134: | ||
::If string starts with a prefix-string, return a copy without it. Otherwise return the original string. |
::If string starts with a prefix-string, return a copy without it. Otherwise return the original string. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello small world' withoutPrefix:'hello ' |
'hello small world' withoutPrefix:'hello ' |
||
=> ' small world ' |
=> ' small world ' |
||
Zeile 138: | Zeile 140: | ||
'small world' withoutPrefix:'hello ' |
'small world' withoutPrefix:'hello ' |
||
=> ' small world ' |
=> ' small world ' |
||
</div> |
|||
''aString'' <code>withoutSuffix:</code>''suffixString'' [ <code>caseSensitive:</code> ''boolean' ]' |
''aString'' <code>withoutSuffix:</code>''suffixString'' [ <code>caseSensitive:</code> ''boolean' ]' |
||
<br>''aString''<code>.withoutSuffix[_ caseSensitive](</code> ''suffixString'' [, ''boolean'' <code>)</code> [JS] |
<br>''aString''<code>.withoutSuffix[_ caseSensitive](</code> ''suffixString'' [, ''boolean'' <code>)</code> [JS] |
||
::If string ends with a prefix-string, return a copy without it. Otherwise return the original string. |
::If string ends with a prefix-string, return a copy without it. Otherwise return the original string. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello small world' withoutSuffix:' world' |
'hello small world' withoutSuffix:' world' |
||
=> ' hello small' |
=> ' hello small' |
||
Zeile 148: | Zeile 151: | ||
'hello small' withoutSuffix:' world' |
'hello small' withoutSuffix:' world' |
||
=> ' hello small ' |
=> ' hello small ' |
||
</div> |
|||
=== Concatenation === |
=== Concatenation === |
||
Zeile 155: | Zeile 158: | ||
::concatenates two strings |
::concatenates two strings |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello', ' ' , 'world' |
'hello', ' ' , 'world' |
||
=> 'hello world' |
=> 'hello world' |
||
</div> |
|||
=== Splitting === |
=== Splitting === |
||
Zeile 164: | Zeile 168: | ||
::Splits a string into pieces, given a splitting character. |
::Splits a string into pieces, given a splitting character. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello world here are six words' splitBy: $ <- trailing space here |
'hello world here are six words' splitBy: $ <- trailing space here |
||
=> #( 'hello' 'world' 'here' 'are' 'six' 'words') |
=> #( 'hello' 'world' 'here' 'are' 'six' 'words') |
||
Zeile 169: | Zeile 174: | ||
'hello-world here are six-words' splitBy: $- |
'hello-world here are six-words' splitBy: $- |
||
=> #( 'hello' 'world here are six' 'words') |
=> #( 'hello' 'world here are six' 'words') |
||
</div> |
|||
''aString'' <code>splitOn:</code>''aCharacter'' |
''aString'' <code>splitOn:</code>''aCharacter'' |
||
<br>''string1'' <code>splitOn:</code>''string2'' |
<br>''string1'' <code>splitOn:</code>''string2'' |
||
Zeile 181: | Zeile 186: | ||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello world here are six words' splitOn: $ <- trailing space here |
'hello world here are six words' splitOn: $ <- trailing space here |
||
=> #( 'hello' 'world' 'here' 'are' 'six' 'words') |
=> #( 'hello' 'world' 'here' 'are' 'six' 'words') |
||
Zeile 213: | Zeile 219: | ||
"hello-world, commas and semis; here".splitOn( (ch) => (ch == $',') || ( ch == $';') ) |
"hello-world, commas and semis; here".splitOn( (ch) => (ch == $',') || ( ch == $';') ) |
||
=> [ "hello-world" , "çommas and semis" , "here"] |
=> [ "hello-world" , "çommas and semis" , "here"] |
||
</div> |
|||
=== Case Conversion === |
=== Case Conversion === |
||
Zeile 224: | Zeile 230: | ||
::Covert to lowercase, uppercase |
::Covert to lowercase, uppercase |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO' asLowercase |
'HELLO' asLowercase |
||
=> 'hello' |
=> 'hello' |
||
Zeile 232: | Zeile 239: | ||
'hello' asUppercaseFirst |
'hello' asUppercaseFirst |
||
=> 'Hello' |
=> 'Hello' |
||
</div> |
|||
=== Comparing === |
=== Comparing === |
||
Zeile 240: | Zeile 247: | ||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO' sameAs: 'HeLlo' |
'HELLO' sameAs: 'HeLlo' |
||
=> true |
=> true |
||
Zeile 248: | Zeile 256: | ||
'HELLO' caselessEqual: 'HeLlo' |
'HELLO' caselessEqual: 'HeLlo' |
||
=> true |
=> true |
||
</div> |
|||
''string1'' <code>caselessBefore:</code>''string2'' |
''string1'' <code>caselessBefore:</code>''string2'' |
||
<br>''string1'' <code>caselessAfter:</code>''string2'' |
<br>''string1'' <code>caselessAfter:</code>''string2'' |
||
Zeile 256: | Zeile 264: | ||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO' caselessBefore: 'world' |
'HELLO' caselessBefore: 'world' |
||
=> true |
=> true |
||
Zeile 261: | Zeile 270: | ||
'bce' caselessBefore: 'abc' |
'bce' caselessBefore: 'abc' |
||
=> false |
=> false |
||
</div> |
|||
''string1'' <code>startsWith:</code>''prefixString'' |
''string1'' <code>startsWith:</code>''prefixString'' |
||
<br>''string1'' <code>startsWith:</code>''prefixString'' <code>caseSensitive:</code> ''boolean'' |
<br>''string1'' <code>startsWith:</code>''prefixString'' <code>caseSensitive:</code> ''boolean'' |
||
Zeile 269: | Zeile 278: | ||
Comes in two variants, one being strict, the other with optional case-insensitivity. |
Comes in two variants, one being strict, the other with optional case-insensitivity. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello' startsWith: 'hel' |
'hello' startsWith: 'hel' |
||
=> true |
=> true |
||
Zeile 280: | Zeile 290: | ||
'Hexlo' startsWith: 'hel' caseSensitive: false |
'Hexlo' startsWith: 'hel' caseSensitive: false |
||
=> false |
=> false |
||
</div> |
|||
''string1'' <code>endsWith:</code>''prefixString'' |
''string1'' <code>endsWith:</code>''prefixString'' |
||
Zeile 286: | Zeile 296: | ||
::Checks if a string ends with another string.<br>Comes in two variants, one being strict, the other with optional case-insensitivity. |
::Checks if a string ends with another string.<br>Comes in two variants, one being strict, the other with optional case-insensitivity. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'hello' endsWith: 'lo' |
'hello' endsWith: 'lo' |
||
=> true |
=> true |
||
Zeile 297: | Zeile 308: | ||
'Hexlo' endsWith: 'LX' caseSensitive: false |
'Hexlo' endsWith: 'LX' caseSensitive: false |
||
=> false |
=> false |
||
</div> |
|||
=== Searching === |
=== Searching === |
||
Zeile 304: | Zeile 315: | ||
::Returns the first/last index of an element (a character). Returns 0 if not found. |
::Returns the first/last index of an element (a character). Returns 0 if not found. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO' indexOf: $L |
'HELLO' indexOf: $L |
||
=> 3 |
=> 3 |
||
Zeile 312: | Zeile 324: | ||
'HELLO' lastIndexOf: $L |
'HELLO' lastIndexOf: $L |
||
=> 4 |
=> 4 |
||
</div> |
|||
''aString'' <code>indexOf:</code>''aCharacter'' <code>startingAt:</code>''startIndex'' |
''aString'' <code>indexOf:</code>''aCharacter'' <code>startingAt:</code>''startIndex'' |
||
<br>''aString'' <code>lastIndexOf:</code>''aCharacter'' <code>startingAt:</code>''startIndex'' |
<br>''aString'' <code>lastIndexOf:</code>''aCharacter'' <code>startingAt:</code>''startIndex'' |
||
::Returns the next/previous index of an element (a character) given a search start index .<br>Returns 0 if not found. |
::Returns the next/previous index of an element (a character) given a search start index .<br>Returns 0 if not found. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO WORLD' indexOf: $O startingAt: 6 |
'HELLO WORLD' indexOf: $O startingAt: 6 |
||
=> 8 |
=> 8 |
||
Zeile 328: | Zeile 341: | ||
'HELLO WORLD' lastIndexOf: $O startingAt: 7 |
'HELLO WORLD' lastIndexOf: $O startingAt: 7 |
||
=> 5 |
=> 5 |
||
</div> |
|||
''aString'' <code>indexOfAny:</code>''aCollectionOfCharacters'' [ <code>startingAt:</code>''startIndex'' ] |
''aString'' <code>indexOfAny:</code>''aCollectionOfCharacters'' [ <code>startingAt:</code>''startIndex'' ] |
||
Zeile 334: | Zeile 347: | ||
::Similar to the above, but searches for any element in the given argument collection. This may be a string (of characters) or an array or any other collection of characters.<br>Returns 0 if not found. |
::Similar to the above, but searches for any element in the given argument collection. This may be a string (of characters) or an array or any other collection of characters.<br>Returns 0 if not found. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO, WORLD' indexOfAny: ',;' |
'HELLO, WORLD' indexOfAny: ',;' |
||
=> 6 |
=> 6 |
||
Zeile 345: | Zeile 359: | ||
'HELLO. WORLD' indexOfAny: #( $, $; ) |
'HELLO. WORLD' indexOfAny: #( $, $; ) |
||
=> 0 |
=> 0 |
||
</div> |
|||
''aString'' <code>indexOfSeparator</code> |
''aString'' <code>indexOfSeparator</code> |
||
Zeile 351: | Zeile 365: | ||
::Searches for a whitespace character (Space, Tab, CR or NL).<br>Returns 0 if not found. |
::Searches for a whitespace character (Space, Tab, CR or NL).<br>Returns 0 if not found. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO WORLD' indexOfSeparator |
'HELLO WORLD' indexOfSeparator |
||
=> 6 |
=> 6 |
||
Zeile 356: | Zeile 371: | ||
'HELLO WORLD' indexOfSeparatorStartingAt: 7 |
'HELLO WORLD' indexOfSeparatorStartingAt: 7 |
||
=> 0 |
=> 0 |
||
</div> |
|||
''aString'' <code>indexOfString:</code>''aSubstring'' [ <code>startingAt:</code>''startIndex'' ] |
''aString'' <code>indexOfString:</code>''aSubstring'' [ <code>startingAt:</code>''startIndex'' ] |
||
<br>''aString'' <code>lastIndexOfString:</code>''aSubstring'' [ <code>startingAt:</code>''startIndex'' ] |
<br>''aString'' <code>lastIndexOfString:</code>''aSubstring'' [ <code>startingAt:</code>''startIndex'' ] |
||
::Returns the first/last index of an element (a character). Returns 0 if not found. |
::Returns the first/last index of an element (a character). Returns 0 if not found. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO' indexOfString:'LL' |
'HELLO' indexOfString:'LL' |
||
=> 3 |
=> 3 |
||
Zeile 369: | Zeile 385: | ||
'HELLO BELLO' lastIndexOfString: 'LL' |
'HELLO BELLO' lastIndexOfString: 'LL' |
||
=> 9 |
=> 9 |
||
</div> |
|||
=== Pattern Matching === |
=== Pattern Matching === |
||
Zeile 375: | Zeile 391: | ||
::True if a string matches a GLOB match pattern. |
::True if a string matches a GLOB match pattern. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO world' matches: 'HE*' |
'HELLO world' matches: 'HE*' |
||
=> true |
=> true |
||
Zeile 383: | Zeile 400: | ||
'HELLO world' matches: 'he*' caseSensitive: false |
'HELLO world' matches: 'he*' caseSensitive: false |
||
=> true |
=> true |
||
</div> |
|||
''aString'' <code>matchesRegex:</code>''patternString'' [ <code>caseSensitive:</code>''boolean'' ] |
''aString'' <code>matchesRegex:</code>''patternString'' [ <code>caseSensitive:</code>''boolean'' ] |
||
::True if a string matches a regex pattern. |
::True if a string matches a regex pattern. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'HELLO world' matchesRegex: 'H.*O' |
'HELLO world' matchesRegex: 'H.*O' |
||
=> true |
=> true |
||
Zeile 396: | Zeile 414: | ||
'HELLO world' matchesRegex: 'h.*o' caseSensitive: false |
'HELLO world' matchesRegex: 'h.*o' caseSensitive: false |
||
=> true |
=> true |
||
</div> |
|||
=== Converting === |
=== Converting === |
||
Zeile 403: | Zeile 421: | ||
The characters must be within the ISO8859 range 0x00 .. 0xFF. |
The characters must be within the ISO8859 range 0x00 .. 0xFF. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'abc123' asByteArray |
'abc123' asByteArray |
||
=> #[97 98 99 49 50 51] |
=> #[97 98 99 49 50 51] |
||
Zeile 408: | Zeile 427: | ||
'äöü' asByteArray |
'äöü' asByteArray |
||
=> #[228 246 252] |
=> #[228 246 252] |
||
</div> |
|||
''aString'' <code>asFilename</code> |
''aString'' <code>asFilename</code> |
||
::Returns a filename instance, which provides functions top operate on files and directories.<br>See [[#Filename | "Filename protocol"]] for its functions. |
::Returns a filename instance, which provides functions top operate on files and directories.<br>See [[#Filename | "Filename protocol"]] for its functions. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'c:\data.txt' asFilename modificationTime |
'c:\data.txt' asFilename modificationTime |
||
=> 2017-07-28 10:31:23 |
=> 2017-07-28 10:31:23 |
||
Zeile 417: | Zeile 437: | ||
'/etc' asFilename exists |
'/etc' asFilename exists |
||
=> true |
=> true |
||
<div style="margin-left: 2em;"> |
|||
</div> |
|||
''aStringWithMultipleLines'' <code>asCollectionOfLines</code> |
''aStringWithMultipleLines'' <code>asCollectionOfLines</code> |
||
::Returns a collection of strings, each containing one line from the original string. Handles any combination of CR, LF or CRLF as line separator. The resulting line-collection can then be further processed using functions from the [[#Collection | Collection protocol]]. |
::Returns a collection of strings, each containing one line from the original string. Handles any combination of CR, LF or CRLF as line separator. The resulting line-collection can then be further processed using functions from the [[#Collection | Collection protocol]]. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'data.txt' asFilename contents asCollectionOfLines |
'data.txt' asFilename contents asCollectionOfLines |
||
=> #( 'line1' 'line2' 'line3' ... ) |
=> #( 'line1' 'line2' 'line3' ... ) |
||
</div> |
|||
=== Encoding / Decoding === |
=== Encoding / Decoding === |
||
Zeile 429: | Zeile 451: | ||
::Encode / decode into/from utf8 encoding. |
::Encode / decode into/from utf8 encoding. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'äöü' utf8Encoded |
'äöü' utf8Encoded |
||
=> 'äöü' |
=> 'äöü' |
||
Zeile 437: | Zeile 460: | ||
'äöü' utf8Encoded utf8Decoded |
'äöü' utf8Encoded utf8Decoded |
||
=> 'äöü' |
=> 'äöü' |
||
</div> |
|||
''aString'' <code>base64Encoded</code> |
''aString'' <code>base64Encoded</code> |
||
::Encode / decode into/from base64 encoding. |
::Encode / decode into/from base64 encoding. |
||
::Example: |
::Example: |
||
<div style="margin-left: 2em;"> |
|||
'äöü' base64Encoded |
'äöü' base64Encoded |
||
=> '5Pb8' |
=> '5Pb8' |
||
Zeile 453: | Zeile 477: | ||
'äöü' base64Encoded base64Decoded asString |
'äöü' base64Encoded base64Decoded asString |
||
=> 'äöü' |
=> 'äöü' |
||
</div> |
Version vom 26. September 2020, 02:40 Uhr
This document lists most useful (and most often needed) functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.
Reference: String inherits from: CharacterArray Collection
Notice: unless when written otherwise, all indices are 1-based. Valid indices range from 1 to the string's size.
Inhaltsverzeichnis
Literals (i.e. Constant Strings)[Bearbeiten]
'...'
- Smalltalk style string (as is; no escapes for special characters). Be aware, that this is inconvenient, if newlines, tabs or other non-graphical characters are to be in the string.
c'...'
- C style string (supports the usual C-escapes, such as "\n" for newline, "\t" for tab or "\xHH" for hex codes)
e'...{expr}...'
- C style with embedded expressions. These are evaluated and sliced into the string.
Queries[Bearbeiten]
aString size
aString.size()
[JS]
- Returns the number of characters in the string (i.e. the string's length).
- Example:
'hello world' size => 11
Accessing[Bearbeiten]
aString at:
index
aString[
índex]
[JS]
- Returns the character at an index.
- Example:
'hello world' at:2 => $e
Copying[Bearbeiten]
aString copyFrom:
startIndex to:
endIndex
aString.copyFrom_to(
startIndex, endIndex)
[JS]
- Copies characters from a start index to an end index.
- Example:
'hello world' copyFrom:1 to:5. => 'hello'
aString copyFrom:
startIndex count:
numChars
aString.copyFrom_count(
startIndex, numChars)
[JS]
- Copies a number of characters starting at the given index.
- Example:
'hello world' copyFrom:7 count:3. => 'wor'
aString copyFrom:
startIndex'
aString.copyFrom(
startIndex)
[JS]
- Copies from the given index to the end.
- Example:
'hello world' copyFrom:6. => ' world'
aString copyTo:
endIndex
aString.copyTo(
endIndex )
[JS]
- Copies from the start to the given index.
- Example:
'hello world' copyTo:6. => 'hello '
aString copyLast:
count
aString.copyLast(
count )
[JS]
- Copies the last count characters.
- Example:
'hello world' copyLast:4. => 'world '
aString copyButFirst:
count
aString.copyButFirst(
count )
[JS]
- Copies except for the first count characters.
- Example:
'hello world' copyButFirst:4. => ' world'
aString copyButLast:
count
aString.copyButLast(
count )
[JS]
- Copies except for the last count characters.
- Example:
'hello world' copyButLast:4. => 'hello w'
aString copyBetween:
leftString and:
rightString caseSensitive:
boolean
aString.copyBetween_and_caseSensitive(
leftString, rightString, boolean )
[JS]
- Finds two substrings and copies the part between them.
- Example:
'hello small world' copyBetween:'hello' and:'world' caseSensitive:true => ' small '
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true =>
'hello small World' copyBetween:'hello' and:'world' caseSensitive:true => nil
'hello small World' copyBetween:'hello' and:'world' caseSensitive:false => ' small '
aString copyReplaceString:
oldString withString:
newString
aString.copyReplaceString_withString(
oldString, newString )
[JS]
- Example:
'hello small world' copyReplaceString:'small' withString:'big' => ' hello big world '
'hello small world' copyReplaceString:'big' withString:'bigger' => ' hello small world '
aString withoutPrefix:
prefixString [ caseSensitive:
boolean ]
aString.withoutPrefix[_caseSensitive](
prefixString [, boolean ]) [JS]
- If string starts with a prefix-string, return a copy without it. Otherwise return the original string.
- Example:
'hello small world' withoutPrefix:'hello ' => ' small world '
'small world' withoutPrefix:'hello ' => ' small world '
aString withoutSuffix:
suffixString [ caseSensitive:
boolean' ]'
aString.withoutSuffix[_ caseSensitive](
suffixString [, boolean )
[JS]
- If string ends with a prefix-string, return a copy without it. Otherwise return the original string.
- Example:
'hello small world' withoutSuffix:' world' => ' hello small'
'hello small' withoutSuffix:' world' => ' hello small '
Concatenation[Bearbeiten]
string1 ,
string2
string1 +
string2 [JS]
- concatenates two strings
- Example:
'hello', ' ' , 'world' => 'hello world'
Splitting[Bearbeiten]
aString splitBy:
aCharacter
aString.splitBy(
aCharacter )
[JS]
- Splits a string into pieces, given a splitting character.
- Example:
'hello world here are six words' splitBy: $ <- trailing space here => #( 'hello' 'world' 'here' 'are' 'six' 'words')
'hello-world here are six-words' splitBy: $- => #( 'hello' 'world here are six' 'words')
aString splitOn:
aCharacter
string1 splitOn:
string2
string1 splitOn:
regex
aString splitOn:[:
char | <condition-expression on char> ]
aString.splitOn(
aCharacter )
[JS]
string1.splitOn(
string2 )
[JS]
aString.splitOn(
(char) => <condition-expression on char> )
[JS]
- Splits a string into pieces, given a splitter.
The splitter may be a single character, a string, a regular expression or a block, which returns true to split.
This is a more general version of the above "splitBy:", for complex splits.
- Splits a string into pieces, given a splitter.
- Example:
'hello world here are six words' splitOn: $ <- trailing space here => #( 'hello' 'world' 'here' 'are' 'six' 'words')
'hello-world here are six-words' splitOn: $- => #( 'hello' 'world here are six' 'words')
'hello world and goodbye world' splitOn: ' and ' => #( 'hello world' 'goodbye world')
'hello-world, commas and semis; here' splitOn: [:ch | (ch == $,) or:[ ch == $; ]] => #( 'hello-world' 'çommas and semis' 'here')
'aWordWithCamelCase' splitOn: [:ch | ch isUppercase ] => #( 'a' 'Word' 'With' 'Camel' 'Case')
'aWordWithCamelCase' splitOn: #isUppercase => #( 'a' 'Word' 'With' 'Camel' 'Case')
'123abc456def789' splitOn:'[a-z]*' asRegex => #('123' '456' '789')
"hello world here are six words".splitOn( $' ' ) <- space character here => [ "hello" , "world" , "here" , "are" , "six" , "words" ]
"hello world and goodbye world".splitOn(" and ") => [ "hello world" , "goodbye world"]
"123abc456def789".splitOn("[a-z]*" asRegex) => [ "123" "456" "789" ]
"hello-world, commas and semis; here".splitOn( (ch) => (ch == $',') || ( ch == $';') ) => [ "hello-world" , "çommas and semis" , "here"]
Case Conversion[Bearbeiten]
aString asLowercase
aString asUppercase
aString asUppercaseFirst
aString.asUppercase()
[JS]
aString.asLowercase()
[JS]
aString.asUppercaseFirst()
[JS]
- Covert to lowercase, uppercase
- Example:
'HELLO' asLowercase => 'hello'
'hello' asUppercase => 'HELLO'
'hello' asUppercaseFirst => 'Hello'
Comparing[Bearbeiten]
string1 sameAs:
string2
string1.sameAs(
string2 )
[JS]
- Compares two strings, ignoring case.
"caselessEqual:"is an alias which performs the same operation.
- Compares two strings, ignoring case.
- Example:
'HELLO' sameAs: 'HeLlo' => true
'HELxO' sameAs: 'HeLlo' => false
'HELLO' caselessEqual: 'HeLlo' => true
string1 caselessBefore:
string2
string1 caselessAfter:
string2
string1.caselessBefore(
string2 )
[JS]
string1.caselessAfter(
string2 )
[JS]
- Compare two strings, ignoring case.
- Example:
'HELLO' caselessBefore: 'world' => true
'bce' caselessBefore: 'abc' => false
string1 startsWith:
prefixString
string1 startsWith:
prefixString caseSensitive:
boolean
string1.startsWith(
prefixString )
[JS]
string1.startsWith_caseSensitive(
prefixString, boolean )
[JS]
- Checks if a string starts with another string.
Comes in two variants, one being strict, the other with optional case-insensitivity.
- Example:
'hello' startsWith: 'hel' => true
'Hello' startsWith: 'hel' => false
'Hello' startsWith: 'hel' caseSensitive: false => true
'Hexlo' startsWith: 'hel' caseSensitive: false => false
string1 endsWith:
prefixString
string1 endsWith:
prefixString caseSensitive:
boolean
- Checks if a string ends with another string.
Comes in two variants, one being strict, the other with optional case-insensitivity. - Example:
- Checks if a string ends with another string.
'hello' endsWith: 'lo' => true
'Hello' endsWith: 'Lo' => false
'Hello' endsWith: 'Lo' caseSensitive: false => true
'Hexlo' endsWith: 'LX' caseSensitive: false => false
Searching[Bearbeiten]
aString indexOf:
aCharacter
aString lastIndexOf:
aCharacter
- Returns the first/last index of an element (a character). Returns 0 if not found.
- Example:
'HELLO' indexOf: $L => 3
'HELLO' indexOf: $x => 0
'HELLO' lastIndexOf: $L => 4
aString indexOf:
aCharacter startingAt:
startIndex
aString lastIndexOf:
aCharacter startingAt:
startIndex
- Returns the next/previous index of an element (a character) given a search start index .
Returns 0 if not found. - Example:
- Returns the next/previous index of an element (a character) given a search start index .
'HELLO WORLD' indexOf: $O startingAt: 6 => 8
'HELLO WORLD' indexOf: $x startingAt: 6 => 0
'HELLO WORLD' indexOf: $L startingAt: 6 => 0
'HELLO WORLD' lastIndexOf: $O startingAt: 7 => 5
aStringindexOfAny:
aCollectionOfCharacters [startingAt:
startIndex ]
aString lastIndexOfAny:
aCollectionOfCharacters [ startingAt:
startIndex ]
- Similar to the above, but searches for any element in the given argument collection. This may be a string (of characters) or an array or any other collection of characters.
Returns 0 if not found. - Example:
- Similar to the above, but searches for any element in the given argument collection. This may be a string (of characters) or an array or any other collection of characters.
'HELLO, WORLD' indexOfAny: ',;' => 6
'HELLO; WORLD' indexOfAny: ',;' => 6
'HELLO; WORLD' indexOfAny: #( $, $; ) => 6
'HELLO. WORLD' indexOfAny: #( $, $; ) => 0
aString indexOfSeparator
aString indexOfSeparatorStartingAt:
startIndex
- Searches for a whitespace character (Space, Tab, CR or NL).
Returns 0 if not found. - Example:
- Searches for a whitespace character (Space, Tab, CR or NL).
'HELLO WORLD' indexOfSeparator => 6
'HELLO WORLD' indexOfSeparatorStartingAt: 7 => 0
aString indexOfString:
aSubstring [ startingAt:
startIndex ]
aString lastIndexOfString:
aSubstring [ startingAt:
startIndex ]
- Returns the first/last index of an element (a character). Returns 0 if not found.
- Example:
'HELLO' indexOfString:'LL' => 3
'HELLO' indexOfString: 'LX' => 0
'HELLO BELLO' lastIndexOfString: 'LL' => 9
Pattern Matching[Bearbeiten]
aString matches:
patternString [ caseSensitive:
boolean ]
- True if a string matches a GLOB match pattern.
- Example:
'HELLO world' matches: 'HE*' => true
'HELLO world' matches: 'he*' => false
'HELLO world' matches: 'he*' caseSensitive: false => true
aString matchesRegex:
patternString [ caseSensitive:
boolean ]
- True if a string matches a regex pattern.
- Example:
'HELLO world' matchesRegex: 'H.*O' => true
'HELLO world' matchesRegex: 'h.*o' => false
'HELLO world' matchesRegex: 'h.*o' caseSensitive: false => true
Converting[Bearbeiten]
aString asByteArray
- Returns a byte array containing the codePoints (ISO8859 codes).
The characters must be within the ISO8859 range 0x00 .. 0xFF.
- Example:
'abc123' asByteArray => #[97 98 99 49 50 51]
'äöü' asByteArray => #[228 246 252]
aString asFilename
- Returns a filename instance, which provides functions top operate on files and directories.
See "Filename protocol" for its functions. - Example:
- Returns a filename instance, which provides functions top operate on files and directories.
'c:\data.txt' asFilename modificationTime => 2017-07-28 10:31:23
'/etc' asFilename exists => true
aStringWithMultipleLines asCollectionOfLines
- Returns a collection of strings, each containing one line from the original string. Handles any combination of CR, LF or CRLF as line separator. The resulting line-collection can then be further processed using functions from the Collection protocol.
- Example:
'data.txt' asFilename contents asCollectionOfLines => #( 'line1' 'line2' 'line3' ... )
Encoding / Decoding[Bearbeiten]
aString utf8Encoded
- Encode / decode into/from utf8 encoding.
- Example:
'äöü' utf8Encoded => 'äöü'
'äöü' utf8Encoded asByteArray => #[195 164 195 182 195 188]
'äöü' utf8Encoded utf8Decoded => 'äöü'
aString base64Encoded
- Encode / decode into/from base64 encoding.
- Example:
'äöü' base64Encoded => '5Pb8'
'äöü' base64Encoded asByteArray => #[53 80 98 56]
'äöü' base64Encoded base64Decoded => #[228 246 252]
'äöü' base64Encoded base64Decoded asString => 'äöü'