Useful API Functions: Unterschied zwischen den Versionen
Cg (Diskussion | Beiträge) |
Cg (Diskussion | Beiträge) |
||
Zeile 1: | Zeile 1: | ||
This document lists most useful (and often needed) functions. |
|||
Be aware, that there are many more to be found in either the class references or via the builtin class browser. |
|||
== String == |
== String == |
||
Version vom 27. Juli 2019, 16:04 Uhr
This document lists most useful (and often needed) functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.
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 '
copyLast:[Bearbeiten]
Copies the last count characters.
aString copyLast:count aString.copyLast(count) [JS]
Example:
'hello world' copyLast:4. => 'world '
copyButLast:[Bearbeiten]
Copies except for the last count characters.
aString copyButLast:count aString.copyButLast(count) [JS]
Example:
'hello world' copyButLast:4. => 'hello w '
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') 'aWordWithCamelCase' splitOn: [:ch | ch isUppercase ] => #( 'a' 'Word' 'With' 'Camel' 'Case') 'aWordWithCamelCase' splitOn: #isUppercase => #( 'a' 'Word' 'With' 'Camel' 'Case') "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"]
Case Conversion[Bearbeiten]
asLowercase[Bearbeiten]
aString1 asLowercase string1.asLowercase() [JS]
Example:
'HELLO' asLowercase => 'hello'
asUppercase[Bearbeiten]
aString1 asUppercase string1.asUppercase() [JS]
Example:
'hello' asUppercase => 'HELLO'
asUppercaseFirst[Bearbeiten]
aString1 asUppercaseFirst string1.asUppercaseFirst() [JS]
Example:
'hello' asUppercaseFirst => 'Hello'
Comparing[Bearbeiten]
sameAs:[Bearbeiten]
Compares two strings, ignoring case.
string1 sameAs:'string2' string1.sameAs("string2") [JS]
Example:
'HELLO' sameAs: 'HeLlo' => true 'HELxO' sameAs: 'HeLlo' => false
startsWith:[caseSensitive:][Bearbeiten]
Checks if a string starts with another string. Comes in two variants, one being strict, the other with optional case-insensitivity.
string1 startsWith:prefixString string1 startsWith:prefixString caseSensitive:boolean string1.startsWith( prefixString ) [JS] string1.startsWith_caseSensitive( prefixString, boolean ) [JS]
Example:
'hello' startsWith: 'hel' =>true 'Hello' startsWith: 'hel' =>false 'Hello' startsWith: 'hel' caseSensitive: false =>true 'Hexlo' startsWith: 'hel' caseSensitive: false =>false
endsWith:[caseSensitive:][Bearbeiten]
Checks if a string ends with another string. Comes in two variants, one being strict, the other with optional case-insensitivity.
string1 endsWith:prefixString string1 endsWith:prefixString caseSensitive:boolean string1.endsWith( prefixString ) [JS] string1.endsWith_caseSensitive( prefixString, boolean ) [JS]
Example:
'hello' endsWith: 'lo' =>true 'Hello' endsWith: 'Lo' =>false 'Hello' endsWith: 'Lo' caseSensitive: false =>true 'Hexlo' endsWith: 'LX' caseSensitive: false =>false