Useful API Functions: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) |
Cg (Diskussion | Beiträge) |
||
Zeile 99: | Zeile 99: | ||
=== Splitting === |
=== Splitting === |
||
===== splitBy: ===== |
|||
Splits a string into pieces, given a splitting character. |
|||
''aString'' splitBy: ''aCharacter'' |
|||
''aString''.splitBy(''aCharacter'') [JS] |
|||
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') |
|||
===== splitOn: ===== |
===== splitOn: ===== |
||
Splits a string into pieces, given a splitter. |
Splits a string into pieces, given a splitter. |
||
The splitter may be a single character, a string or a block, which returns true to split. |
The splitter may be a single character, a string or a block, which returns true to split. |
||
This is a more general version of the above "splitBy:", for complex splits. |
|||
''aString'' splitOn: ''aCharacter'' |
''aString'' splitOn: ''aCharacter'' |
Version vom 27. Juli 2019, 15:46 Uhr
Inhaltsverzeichnis
String[Bearbeiten]
Reference: String Class Documentation
CharacterArray (superclass)
Collection (superclass)
Copying[Bearbeiten]
Notice: except when written otherwise, all indices are 1-based. Valid indices range from 1 to the string's size.
copyFrom:to:[Bearbeiten]
Copies characters from a start index to an end index.
aString copyFrom:startIndex to:endIndex aString.copyFrom_to(startIndex, endIndex) [JS]
Example:
'hello world' copyFrom:1 to:5. => 'hello'
copyFrom:count:[Bearbeiten]
Copies a number of characters starting at the given index.
aString copyFrom:startIndex count:numChars aString.copyFrom_count(startIndex, numChars) [JS]
Example:
'hello world' copyFrom:7 count:3. => 'wor'
copyFrom:[Bearbeiten]
Copies from the given index to the end.
aString copyFrom:startIndex' aString.copyFrom(startIndex) [JS]
Example:
'hello world' copyFrom:6. => ' world'
copyTo:[Bearbeiten]
Copies from the start to the given index.
aString copyTo:endIndex aString.copyTo(endIndex) [JS]
Example:
'hello world' copyTo:6. => 'hello '
copyBetween:and:caseSensitive:[Bearbeiten]
Finds two substrings and copies the part between them.
aString copyBetween:leftString and:rightString caseSensitive:boolean aString.copyBetween_and_caseSensitive(leftString, rightString, boolean) [JS]
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 '
copyReplaceString:withString:[Bearbeiten]
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 '
Concatenation[Bearbeiten]
, (comma)[Bearbeiten]
string1 , string2 string1 + string2 [JS]
Example:
'hello', ' ' , 'world' => 'hello world'
Splitting[Bearbeiten]
splitBy:[Bearbeiten]
Splits a string into pieces, given a splitting character.
aString splitBy: aCharacter
aString.splitBy(aCharacter) [JS]
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')
splitOn:[Bearbeiten]
Splits a string into pieces, given a splitter. The splitter may be a single character, a string or a block, which returns true to split. This is a more general version of the above "splitBy:", for complex splits.
aString splitOn: aCharacter string1 splitOn: string2 aString splitOn: [ :char | <condition-expression on char> ]
aString.splitOn(aCharacter) [JS] string1.splitOn(string2) [JS] aString.splitOn( (char) => <condition-expression on char> ) [JS]
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')
"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"] "hello-world, commas and semis; here".splitOn( (ch) => (ch == $',') || ( ch == $';') ) => [ "hello-world" , "çommas and semis" , "here"]