String API Functions: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
 
(37 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
This document lists most useful (and most often needed) functions.
This document lists most useful (and most often needed) string functions.
Be aware, that there are many more to be found in either the class references or via the builtin class browser.
Be aware, that there are many more to be found in either the class references or via the builtin [[Tools_ClassBrowser/en | class browser]].


See also [[Expecco_API/en#String_Handling | "String Handling"]] in [[Expecco_API]].
See also [[Expecco_API/en#String_Handling | "String Handling"]] in [[Expecco_API]].
Zeile 14: Zeile 14:


:'...'
:'...'
::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.
::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. Notice that Smalltalk's plain strings are similar to Python's "''Raw Strings''".


:c'...'
:c'...'
Zeile 27: Zeile 27:
foo := e'{c}; this string contains embedded exprs.\nYes, {b} and {a} are there.'
foo := e'{c}; this string contains embedded exprs.\nYes, {b} and {a} are there.'
foo
foo
-> 'hello; this string contains embedded exprs.
<small>=> 'hello; this string contains embedded exprs.
Yes, true and 1234 are there.'
Yes, true and 1234 are there.'</small>
</div>
</div>


:i'...{expr}...'
:i'...{expr}...'
:: C style with embedded expressions and automatic nationalization. First, it is expanded as a C-style string. Then a translation is looked up in the currently loaded language resources. The template searched has successive expression positions replaced by "%i".
:: C style with embedded expressions and automatic nationalization. First, it is expanded as a C-style string. Then a translation is looked up in the currently loaded language resources. The template searched has successive expression positions replaced by "%i".
::For example, the literal "<code>i'Hello {name}'</code>" will look for a translation of "Hello %1" in the language pack, and may find one of "Hallo %1", "Hola %1", etc. depending on your current language setting. Then the variable "name" is converted to a string and sliced on, finally generating "Hallo Tom" or "Hola Tom" etc.
::For example, the literal "<code>i'Hello {name}'</code>" will look for a translation of "Hello %1" in the language pack, and may find one of "Hallo %1", "Hola %1", etc. depending on your current language setting. Then the variable "name" is converted to a string and sliced in, finally generating "Hallo Tom" or "Hola Tom" etc.
::Translations are loaded from resource files in the "resources" folder.
::Translations are loaded from resource files in the "resources" folder.


Zeile 44: Zeile 44:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' size</code>
'hello world' size</code>
=> 11
<small>=> 11</small>
</div>
</div>


Zeile 51: Zeile 51:


:''aString'' '''isWideString'''
:''aString'' '''isWideString'''
::Same as "aString bitsPerCharacter > 8""
::Same as "<code>aString bitsPerCharacter > 8</code>"
:''aString'' '''containsNon7BitAscii''''
:''aString'' '''containsNon7BitAscii''''
Zeile 65: Zeile 65:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' at:2
'hello world' at:2
=> $e
<small>=> $e</small>
</div>
</div>


Zeile 81: Zeile 81:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copy at:1
'hello world' copy at:1
=> $h
<small>=> $h</small>
</div>
</div>


Zeile 90: Zeile 90:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyFrom:1 to:5.
'hello world' copyFrom:1 to:5.
=> 'hello'
<small>=> 'hello'</small>
</div>
</div>
:''aString'' '''copyFrom:''' ''startIndex'' '''count:''' ''numChars''
:''aString'' '''copyFrom:''' ''startIndex'' '''count:''' ''numChars''
Zeile 98: Zeile 98:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyFrom:7 count:3.
'hello world' copyFrom:7 count:3.
=> 'wor'
<small>=> 'wor'</small>
</div>
</div>
:''aString'' '''copyFrom:''' ''startIndex'''
:''aString'' '''copyFrom:''' ''startIndex'''
Zeile 106: Zeile 106:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyFrom:6.
'hello world' copyFrom:6.
=> ' world'
<small>=> ' world'</small>
</div>
</div>
:''aString'' '''copyTo:''' ''endIndex''
:''aString'' '''copyTo:''' ''endIndex''
Zeile 114: Zeile 114:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyTo:6.
'hello world' copyTo:6.
=> 'hello '
<small>=> 'hello '</small>
</div>
</div>
:''aString'' '''copyLast:''' ''count''
:''aString'' '''copyLast:''' ''count''
Zeile 122: Zeile 122:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyLast:4.
'hello world' copyLast:4.
=> 'world '
<small>=> 'world '</small>
</div>
</div>
:''aString'' '''copyButFirst:''' ''count''
:''aString'' '''copyButFirst:''' ''count''
Zeile 130: Zeile 130:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyButFirst:4.
'hello world' copyButFirst:4.
=> ' world'
<small>=> ' world'</small>
</div>
</div>
:''aString'' '''copyButLast:''' ''count''
:''aString'' '''copyButLast:''' ''count''
Zeile 138: Zeile 138:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world' copyButLast:4.
'hello world' copyButLast:4.
=> 'hello w'
<small>=> 'hello w'</small>
</div>
</div>
:''aString'' '''copyBetween:''' ''leftString'' '''and:''' ''rightString'' '''caseSensitive:''' ''boolean''
:''aString'' '''copyBetween:''' ''leftString'' '''and:''' ''rightString'' '''caseSensitive:''' ''boolean''
Zeile 146: Zeile 146:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello small world' copyBetween:'hello' and:'world' caseSensitive:true
'hello small world' copyBetween:'hello' and:'world' caseSensitive:true
=> ' small '
<small>=> ' small '</small>
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true
=> &apos;&apos; (an empty string)
<small>=> &apos;&apos; (an empty string)</small>
'hello small World' copyBetween:'hello' and:'world' caseSensitive:true
'hello small World' copyBetween:'hello' and:'world' caseSensitive:true
=> nil
<small>=> nil</small>
'hello small World' copyBetween:'hello' and:'world' caseSensitive:false
'hello small World' copyBetween:'hello' and:'world' caseSensitive:false
=> ' small '
<small>=> ' small '</small>
</div>
</div>
:''aString'' '''copyReplaceString:''' ''oldString'' '''withString:''' ''newString''
:''aString'' '''copyReplaceString:''' ''oldString'' '''withString:''' ''newString''
Zeile 162: Zeile 162:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello small world' copyReplaceString:'small' withString:'big'
'hello small world' copyReplaceString:'small' withString:'big'
=> ' hello big world '
<small>=> ' hello big world '</small>
'hello small world' copyReplaceString:'big' withString:'bigger'
'hello small world' copyReplaceString:'big' withString:'bigger'
=> ' hello small world '
<small>=> ' hello small world '</small>
</div>
</div>
:''aString'' '''withoutPrefix:''' ''prefixString'' [ '''caseSensitive:''' ''boolean'' ]
:''aString'' '''withoutPrefix:''' ''prefixString'' [ '''caseSensitive:''' ''boolean'' ]
Zeile 173: Zeile 173:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello small world' withoutPrefix:'hello '
'hello small world' withoutPrefix:'hello '
=> ' small world '
<small>=> ' small world '</small>
'small world' withoutPrefix:'hello '
'small world' withoutPrefix:'hello '
=> ' small world '
<small>=> ' small world '</small>
</div>
</div>
:''aString'' '''withoutSuffix:''' ''suffixString'' [ '''caseSensitive:''' ''boolean' ]'
:''aString'' '''withoutSuffix:''' ''suffixString'' [ '''caseSensitive:''' ''boolean' ]'
Zeile 184: Zeile 184:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello small world' withoutSuffix:' world'
'hello small world' withoutSuffix:' world'
=> ' hello small'
<small>=> ' hello small'</small>
'hello small' withoutSuffix:' world'
'hello small' withoutSuffix:' world'
=> ' hello small '
<small>=> ' hello small '</small>
</div>
</div>


Zeile 195: Zeile 195:
:''string1'' <code>+</code> ''string2'' [JS]
:''string1'' <code>+</code> ''string2'' [JS]
::the Smalltalk comma operator<sup>1</sup> and the JavaScript plus operator concatenate two strings
::the Smalltalk comma operator<sup>1</sup> and the JavaScript plus operator concatenate two strings
::Example (Smalltalk):
<div style="margin-left: 4em;">
'hello' , ' ' , 'world'
<small>=> 'hello world'</small>
</div>
::Example (JavaScript):
<div style="margin-left: 4em;">
"hello" + " " + "world"
<small>=> "hello world"</small>
</div>

:''string'' <code>,*</code> ''integer''
::the Smalltalk comma-star operator generates a new string by repeating the first string a number of times
::Example:
::Example:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello', ' ' , 'world'
'abc' ,* 5
<small>=> 'abcabcabcabcabc'</small>
=> 'hello world'
</div>
</div>

<sup>1)</sup>Although in theory, the plus operator could have been overloaded in Smalltalk as well, this was not done for readability reasons: using a comma makes it clear that the intention is string concatenation and not addition.
<sup>1)</sup>Although in theory, the plus operator could have been overloaded in Smalltalk as well, this was not done for readability and debuggability reasons: using a comma makes it clear that the intention is string concatenation and not addition.
Also notice, that the JavaScript semantics can be considered unclean, as it concatenates strings if either operand is a string. Thus, you have to be careful if addition is attempted, and either incoming operand could possibly be a string. In contrast, Smalltalk will report an error if you try to concatenate or add strings and numbers.


=== Splitting ===
=== Splitting ===
Zeile 210: Zeile 225:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello world here are six words' splitBy: $ <- trailing invisible space here
'hello world here are six words' splitBy: $ <- trailing invisible space here
=> #( 'hello' 'world' 'here' 'are' 'six' 'words')
<small>=> #( 'hello' 'world' 'here' 'are' 'six' 'words')</small>
'hello world here are six words' splitBy: (Character space) <- no invisible space
'hello world here are six words' splitBy: (Character space) <- no invisible space
=> #( 'hello' 'world' 'here' 'are' 'six' 'words')
<small>=> #( 'hello' 'world' 'here' 'are' 'six' 'words')</small>
'hello-world here are six-words' splitBy: $-
'hello-world here are six-words' splitBy: $-
=> #( 'hello' 'world here are six' 'words')
<small>=> #( 'hello' 'world here are six' 'words')</small>
</div>
</div>
:''aString'' '''splitOn:''' ''aCharacter''
:''aString'' '''splitOn:''' ''aCharacter''
Zeile 230: Zeile 245:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello here are five words' splitOn: $ <- trailing space here
'hello here are five words' splitOn: $ <- trailing space here
=> #( 'hello' 'here' 'are' 'five' 'words')
<small>=> #( 'hello' 'here' 'are' 'five' 'words')</small>
'hello-world here are six-words' splitOn: $-
'hello-world here are six-words' splitOn: $-
=> #( 'hello' 'world here are six' 'words')
<small>=> #( 'hello' 'world here are six' 'words')</small>
'hello world and goodbye world' splitOn: ' and '
'hello world and goodbye world' splitOn: ' and '
=> #( 'hello world' 'goodbye world')
<small>=> #( 'hello world' 'goodbye world')</small>
'hello, commas and semis; here' splitOn: [:ch | (ch == $,) or:[ ch == $; ]]
'hello, commas and semis; here' splitOn: [:ch | (ch == $,) or:[ ch == $; ]]
=> #( 'hello' 'commas and semis' 'here')
<small>=> #( 'hello' 'commas and semis' 'here')</small>
'aWordWithCamelCase' splitOn: [:ch | ch isUppercase ]
'aWordWithCamelCase' splitOn: [:ch | ch isUppercase ]
=> #( 'a' 'Word' 'With' 'Camel' 'Case')
<small>=> #( 'a' 'Word' 'With' 'Camel' 'Case')</small>
'aWordWithCamelCase' splitOn: #isUppercase
'aWordWithCamelCase' splitOn: #isUppercase
=> #( 'a' 'Word' 'With' 'Camel' 'Case')
<small>=> #( 'a' 'Word' 'With' 'Camel' 'Case')</small>
c'some text\twith\ndifferent separators' splitOn: #isSeparator
c'some text\twith\ndifferent separators' splitOn: #isSeparator
=> #( 'some' 'text' 'with' 'different' 'separators')
<small>=> #( 'some' 'text' 'with' 'different' 'separators')</small>
'123abc456def789' splitOn:'[a-z]*' asRegex
'123abc456def789' splitOn:'[a-z]*' asRegex
=> #('123' '456' '789')
<small>=> #('123' '456' '789')</small>
JS examples:
"hello here are five words".splitOn( $' ' ) <- space character here
"hello here are five words".splitOn( $' ' ) <- space character here
=> [ "hello" , "here" , "are" , "five" , "words" ]
<small>=> [ "hello" , "here" , "are" , "five" , "words" ]</small>
"hello here are five words".splitOn( ' ' ) <- string with a space here
<small>=> [ "hello" , "here" , "are" , "five" , "words" ]</small>
"hello world and goodbye world".splitOn(" and ")
"hello world and goodbye world".splitOn(" and ")
=> [ "hello world" , "goodbye world"]
<small>=> [ "hello world" , "goodbye world"]</small>
"123abc456def789".splitOn("[a-z]*" asRegex)
"123abc456def789".splitOn("[a-z]*" asRegex)
=> [ "123" "456" "789" ]
<small>=> [ "123" "456" "789" ]</small>
"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"]
<small>=> [ "hello-world" , "çommas and semis" , "here"]</small>
</div>
</div>


Zeile 278: Zeile 297:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' asLowercase
'HELLO' asLowercase
=> 'hello'
<small>=> 'hello'</small>
'hello' asUppercase
'hello' asUppercase
=> 'HELLO'
<small>=> 'HELLO'</small>
'hello' asUppercaseFirst
'hello' asUppercaseFirst
=> 'Hello'
<small>=> 'Hello'</small>
</div>
</div>


Zeile 291: Zeile 310:
:''string1'' '''sameAs:''' ''string2''
:''string1'' '''sameAs:''' ''string2''
:''string1'''''.sameAs(''' ''string2'' ''')''' &nbsp; [JS]
:''string1'''''.sameAs(''' ''string2'' ''')''' &nbsp; [JS]
::Compares two strings, ignoring case.<br>"caselessEqual:" is an alias which performs the same operation.
::Compares two strings, ignoring case.<br>"'''caselessEqual:'''" is an alias which performs the same operation.


::Example:
::Example:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' sameAs: 'HeLlo'
'HELLO' sameAs: 'HeLlo'
=> true
<small>=> true</small>
'HELxO' sameAs: 'HeLlo'
'HELxO' sameAs: 'HeLlo'
=> false
<small>=> false</small>
'HELLO' caselessEqual: 'HeLlo'
'HELLO' caselessEqual: 'HeLlo'
=> true
<small>=> true</small>
</div>
</div>
:''string1'' '''caselessBefore:''' ''string2''
:''string1'' '''caselessBefore:''' ''string2''
Zeile 309: Zeile 328:
:''string1'''''.caselessBefore(''' ''string2'' ''')''' &nbsp; [JS]
:''string1'''''.caselessBefore(''' ''string2'' ''')''' &nbsp; [JS]
:''string1'''''.caselessAfter(''' ''string2'' ''')''' &nbsp; [JS]
:''string1'''''.caselessAfter(''' ''string2'' ''')''' &nbsp; [JS]
::Compare two strings, ignoring case.<br>Returns true if string1 is smaller, same or larger than string2.<br>Does not care for national characters (i.e. the compare is based on the unicode points)
::Compare two strings, ignoring case.


::Example:
::Example:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' caselessBefore: 'world'
'HELLO' caselessBefore: 'world'
=> true
<small>=> true</small>
'bce' caselessBefore: 'abc'
'bce' caselessBefore: 'abc'
=> false
<small>=> false</small>
</div>
</div>
:''string1'' '''startsWith:''' ''prefixString''
:''string1'' '''startsWith:''' ''prefixString''
Zeile 328: Zeile 347:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello' startsWith: 'hel'
'hello' startsWith: 'hel'
=> true
<small>=> true</small>
'Hello' startsWith: 'hel'
'Hello' startsWith: 'hel'
=> false
<small>=> false</small>
'Hello' startsWith: 'hel' caseSensitive: false
'Hello' startsWith: 'hel' caseSensitive: false
=> true
<small>=> true</small>
'Hexlo' startsWith: 'hel' caseSensitive: false
'Hexlo' startsWith: 'hel' caseSensitive: false
=> false
<small>=> false</small>
</div>
</div>


Zeile 346: Zeile 365:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello' endsWith: 'lo'
'hello' endsWith: 'lo'
=> true
<small>=> true</small>
'Hello' endsWith: 'Lo'
'Hello' endsWith: 'Lo'
=> false
<small>=> false</small>
'Hello' endsWith: 'Lo' caseSensitive: false
'Hello' endsWith: 'Lo' caseSensitive: false
=> true
<small>=> true</small>
'Hexlo' endsWith: 'LX' caseSensitive: false
'Hexlo' endsWith: 'LX' caseSensitive: false
=> false
<small>=> false</small>
</div>
</div>


Zeile 366: Zeile 385:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' indexOf: $L
'HELLO' indexOf: $L
=> 3
<small>=> 3</small>
'HELLO' indexOf: $x
'HELLO' indexOf: $x
=> 0
<small>=> 0</small>
'HELLO' lastIndexOf: $L
'HELLO' lastIndexOf: $L
=> 4
<small>=> 4</small>
</div>
</div>
:''aString'' '''indexOf:''' ''aCharacter'' '''startingAt:''' ''startIndex''
:''aString'' '''indexOf:''' ''aCharacter'' '''startingAt:''' ''startIndex''
Zeile 380: Zeile 399:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO WORLD' indexOf: $O startingAt: 6
'HELLO WORLD' indexOf: $O startingAt: 6
=> 8
<small>=> 8</small>
'HELLO WORLD' indexOf: $x startingAt: 6
'HELLO WORLD' indexOf: $x startingAt: 6
=> 0
<small>=> 0</small>
'HELLO WORLD' indexOf: $L startingAt: 6
'HELLO WORLD' indexOf: $L startingAt: 6
=> 0
<small>=> 0</small>
'HELLO WORLD' lastIndexOf: $O startingAt: 7
'HELLO WORLD' lastIndexOf: $O startingAt: 7
=> 5
<small>=> 5</small>
</div>
</div>
Zeile 398: Zeile 417:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO, WORLD' indexOfAny: ',;'
'HELLO, WORLD' indexOfAny: ',;'
=> 6
<small>=> 6</small>
'HELLO; WORLD' indexOfAny: ',;'
'HELLO; WORLD' indexOfAny: ',;'
=> 6
<small>=> 6</small>
'HELLO; WORLD' indexOfAny: #( $, $; )
'HELLO; WORLD' indexOfAny: #( $, $; )
=> 6
<small>=> 6</small>
'HELLO. WORLD' indexOfAny: #( $, $; )
'HELLO. WORLD' indexOfAny: #( $, $; )
=> 0
<small>=> 0</small>
</div>
</div>
Zeile 418: Zeile 437:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO WORLD' indexOfSeparator
'HELLO WORLD' indexOfSeparator
=> 6
<small>=> 6</small>
'HELLO WORLD' indexOfSeparatorStartingAt: 7
'HELLO WORLD' indexOfSeparatorStartingAt: 7
=> 0
<small>=> 0</small>

'HELLO abc World' lastIndexOfSeparator
'HELLO abc World' lastIndexOfSeparator
=> 10
<small>=> 10</small>

'HELLO abc World' lastIndexOfSeparatorStartingAt:10
'HELLO abc World' lastIndexOfSeparatorStartingAt:10
=> 10
<small>=> 10</small>

'HELLO abc World' lastIndexOfSeparatorStartingAt:9
'HELLO abc World' lastIndexOfSeparatorStartingAt:9
=> 6
<small>=> 6</small>
</div>
</div>
:''aString'' '''indexOfString:''' ''aSubstring'' [ '''startingAt:''' ''startIndex'' ]
:''aString'' '''indexOfString:''' ''aSubstring'' [ '''startingAt:''' ''startIndex'' ]
Zeile 438: Zeile 457:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' indexOfString:'LL'
'HELLO' indexOfString:'LL'
=> 3
<small>=> 3</small>
'HELLO' indexOfString: 'LX'
'HELLO' indexOfString: 'LX'
=> 0
<small>=> 0</small>
'HELLO BELLO' lastIndexOfString: 'LL'
'HELLO BELLO' lastIndexOfString: 'LL'
=> 9
<small>=> 9</small>
</div>
</div>
:''aString'' '''includesString:''' ''aSubstring'' [ '''caseSensitive:''' ''boolean'' ]
:''aString'' '''includesString:''' ''aSubstring'' [ '''caseSensitive:''' ''boolean'' ]
Zeile 451: Zeile 470:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO' includesString:'LL'
'HELLO' includesString:'LL'
=> true
<small>=> true</small>
'HELLO' includesString: 'll'
'HELLO' includesString: 'll'
=> false
<small>=> false</small>
'HELLO' includesString: 'll' caseSensitive: false
'HELLO' includesString: 'll' caseSensitive: false
=> true
<small>=> true</small>
</div>
</div>


Zeile 467: Zeile 486:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO world' matches: 'HE*'
'HELLO world' matches: 'HE*'
=> true
<small>=> true</small>
'HELLO world' matches: 'he*'
'HELLO world' matches: 'he*'
=> false
<small>=> false</small>
'HELLO world' matches: 'he*' caseSensitive: false
'HELLO world' matches: 'he*' caseSensitive: false
=> true
<small>=> true</small>
</div>
</div>
Zeile 481: Zeile 500:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'HELLO world' matchesRegex: 'H.*O'
'HELLO world' matchesRegex: 'H.*O'
=> true
<small>=> true</small>
'HELLO world' matchesRegex: 'h.*o'
'HELLO world' matchesRegex: 'h.*o'
=> false
<small>=> false</small>
'HELLO world' matchesRegex: 'h.*o' caseSensitive: false
'HELLO world' matchesRegex: 'h.*o' caseSensitive: false
=> true
<small>=> true</small>

('myFile.txt' asFilename contents) select:[:line |
line matchesRegex:'[0-9]+\:.*'
]
<small>=> all lines from the file which start with a number followed by a colon.</small>
</div>

:''patternString'' '''regexMatchesIn:''' ''aString''
::Returns a collection containing all [[Regex Pattern Info|regex]] matching substrings.
::Example
<div style="margin-left: 4em;">
'[0-9]+' regexMatchesIn: '1234 abcd 3456 defg'
<small>=> OrderedCollection('1234' '3456')</small>
</div>

:''aString'' '''subExpressionsInRegex:''' ''patternString'' '''caseSensitive:''' ''boolean''
::Returns a collection containing the individual partial matches. Partials are [[Regex Pattern Info|regex]] subexpressions in parentheses.
::Example
<div style="margin-left: 4em;">
'1234abc3456' subExpressionsInRegex: '([0-9]+)abc([0-9]+)' caseSensitive:false
<small>=> OrderedCollection('1234' '3456')</small>
'123 abc xyz :bla' subExpressionsInRegex: '([0-9]+).*(\:.*)' caseSensitive:false
<small>=> OrderedCollection('123' 'bla')</small>
</div>
::Notice that ':' is a special character in the pattern and must therefore be escaped with a backslash.

:''patternStrings'' '''compoundMatch:''' ''aString'' [ '''caseSensitive:''' ''boolean'' ]
::True if a string matches any pattern in patternStrings, which contains multiple [[Regex Pattern Info|GLOB match patterns]] separated by semicolon.
::Useful eg. to verify a string against multiple file extensions (eg. <code>'*.txt;*.doc;*.pdf' compoundMatch: aFilename</code>).
::Example:
<div style="margin-left: 4em;">
'f*;b*' compoundMatch:'foo'
<small>=> true</small>
'f*;b*' compoundMatch:'bar'
<small>=> true</small>
'f*;b*' compoundMatch:'xxx'
<small>=> false</small>
</div>
</div>


Zeile 493: Zeile 552:


:''aString'' '''asByteArray'''
:''aString'' '''asByteArray'''
::Returns a byte array containing the codePoints (ISO8859 codes). The characters must be within the ISO8859 range 0x00 .. 0xFF.
::Returns a byte array containing the codePoints (Unicode codes).
::The characters should be within the ISO8859 range 0x00 .. 0xFF.
::If they are not, a byte array containing the wide characters is returned (to be exact: in the machine's native byte order, which is low byte first on x86 cpus).
::Example:
::Example:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'abc123' asByteArray
'abc123' asByteArray
=> #[97 98 99 49 50 51]
<small>=> #[97 98 99 49 50 51]</small>
'&auml;&ouml;&uuml;' asByteArray
'&auml;&ouml;&uuml;' asByteArray
=> #[228 246 252]
<small>=> #[228 246 252]</small>
c'abc\u2022123' asByteArray
<small>=> #[97 0 98 0 99 0 34 32 49 0 50 0 51 0]</small>
</div>
</div>
:''aString'' '''asFilename'''
:''aString'' '''asFilename'''
::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 to operate on files and directories.<br>See [[#Filename | "Filename protocol"]] for its functions.
::Example:
::Example:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'c:\data.txt' asFilename modificationTime
'c:\data.txt' asFilename modificationTime
=> 2017-07-28 10:31:23
<small>=> 2017-07-28 10:31:23</small>
'/etc' asFilename exists
'/etc' asFilename exists
=> true
<small>=> true</small>
</div>
</div>
:''aStringWithMultipleLines'' '''asCollectionOfLines'''
:''aStringWithMultipleLines'' '''asCollectionOfLines'''
Zeile 517: Zeile 581:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'data.txt' asFilename contents asCollectionOfLines
'data.txt' asFilename contents asCollectionOfLines
=> #( 'line1' 'line2' 'line3' ... )
<small>=> #( 'line1' 'line2' 'line3' ... )</small>
</div>
</div>
:''aString'' '''asCollectionOfWords'''
:''aString'' '''asCollectionOfWords'''
Zeile 524: Zeile 588:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello bla bla world' asCollectionOfWords
'hello bla bla world' asCollectionOfWords
=> #( 'hello' 'bla' 'bla' 'world' )
<small>=> #( 'hello' 'bla' 'bla' 'world' )</small>
</div>
</div>


Zeile 535: Zeile 599:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'&auml;&ouml;&uuml;' utf8Encoded
'&auml;&ouml;&uuml;' utf8Encoded
=> 'äöü'
<small>=> 'äöü'</small>
'&auml;&ouml;&uuml;' utf8Encoded asByteArray
'&auml;&ouml;&uuml;' utf8Encoded asByteArray
=> #[195 164 195 182 195 188]
<small>=> #[195 164 195 182 195 188]</small>
'&auml;&ouml;&uuml;' utf8Encoded utf8Decoded
'&auml;&ouml;&uuml;' utf8Encoded utf8Decoded
=> '&auml;&ouml;&uuml;'
<small>=> '&auml;&ouml;&uuml;'</small>
</div>
</div>
Zeile 549: Zeile 613:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'hello &auml;&ouml;&uuml;' utf16Encoded encodeInto:#utfLE16
'hello &auml;&ouml;&uuml;' utf16Encoded encodeInto:#utfLE16
=> #[104 0 101 0 108 0 108 0 111 0 32 0 228 0 246 0 252 0]
<small>=> #[104 0 101 0 108 0 108 0 111 0 32 0 228 0 246 0 252 0]</small>
</div>
</div>
Zeile 557: Zeile 621:


:''aString'' '''encodeFrom:''' ''nameOfEncoding1'' '''into:''' ''nameOfEncoding2''
:''aString'' '''encodeFrom:''' ''nameOfEncoding1'' '''into:''' ''nameOfEncoding2''
::to transcode from one encoding into another. Eg. "someString encodeFrom:#cp850 into:#koi7" would recode from the Windows codepage 850 into cyrillic koi7.
::to transcode from one encoding into another. E.g. "someString encodeFrom:#cp850 into:#koi7" would recode from the Windows codepage 850 into cyrillic koi7.


:aString'' '''base64Encoded'''
:aString'' '''base64Encoded'''
Zeile 566: Zeile 630:
<div style="margin-left: 4em;">
<div style="margin-left: 4em;">
'&auml;&ouml;&uuml;' base64Encoded
'&auml;&ouml;&uuml;' base64Encoded
=> '5Pb8'
<small>=> '5Pb8'</small>
'&auml;&ouml;&uuml;' base64Encoded asByteArray
'&auml;&ouml;&uuml;' base64Encoded asByteArray
=> #[53 80 98 56]
<small>=> #[53 80 98 56]</small>
'&auml;&ouml;&uuml;' base64Encoded base64Decoded
'&auml;&ouml;&uuml;' base64Encoded base64Decoded
=> #[228 246 252]
<small>=> #[228 246 252]</small>
'&auml;&ouml;&uuml;' base64Encoded base64DecodedString
'&auml;&ouml;&uuml;' base64Encoded base64DecodedString
=> '&auml;&ouml;&uuml;'
<small>=> '&auml;&ouml;&uuml;'</small>
'&auml;&ouml;&uuml;' base64Encoded base64Decoded asString
'&auml;&ouml;&uuml;' base64Encoded base64Decoded asString
=> '&auml;&ouml;&uuml;'
<small>=> '&auml;&ouml;&uuml;'</small>
</div>
</div>



Aktuelle Version vom 30. März 2025, 13:30 Uhr

This document lists most useful (and most often needed) string functions. Be aware, that there are many more to be found in either the class references or via the builtin class browser.

See also "String Handling" in Expecco_API.
Reference: String inherits from: CharacterArray Collection

Back to Useful API Functions

Point right.png Notice: unless when written otherwise, all indices are 1-based. Valid indices range from 1 to the string's size.

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. Notice that Smalltalk's plain strings are similar to Python's "Raw Strings".
c'...'
C style string (supports the usual C-escapes, such as "\n" for newline, "\t" for tab or "\xHH" and "\uHHHH" for hex codes). Backslashes and single quotes must be prefixed by a backslash (which is especially needed when a string represents a Windows pathname). For example, the literal c'abc\u2021def' represents the characters "abc‡def".
e'...{expr}...'
C style with embedded expressions. In addition to C-style escapes, expressions in braces are embedded. These expressions are evaluated and their string representation (i.e. the printString, which is Smalltalk's equivalent of toString in JS) sliced into the string.
Example:
a := 1234.
b := true.
c := 'hello'.
foo := e'{c}; this string contains embedded exprs.\nYes, {b} and {a} are there.'
foo
   => 'hello; this string contains embedded exprs.
       Yes, true and 1234 are there.'
i'...{expr}...'
C style with embedded expressions and automatic nationalization. First, it is expanded as a C-style string. Then a translation is looked up in the currently loaded language resources. The template searched has successive expression positions replaced by "%i".
For example, the literal "i'Hello {name}'" will look for a translation of "Hello %1" in the language pack, and may find one of "Hallo %1", "Hola %1", etc. depending on your current language setting. Then the variable "name" is converted to a string and sliced in, finally generating "Hallo Tom" or "Hola Tom" etc.
Translations are loaded from resource files in the "resources" folder.

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
aString bitsPerCharacter
Returns one of {8, 16, 32}.
aString isWideString
Same as "aString bitsPerCharacter > 8"
aString containsNon7BitAscii'
true if aString is either a wide string, or contains any character above the 7bit Ascii range (i.e. a national character in the ISO8859 charset)

Accessing[Bearbeiten]

aString at: index
aString [ índex ]   [ST/X *]
aString [ índex ]   [JS]
Returns the character at an index (1-based; valid indices are [1 .. size]).
Example:
 'hello world' at:2
     => $e
aString at: index put: char
aString [ índex ] := char   [ST/X *]
aString [ índex ] = char   [JS]
Changes the character at an index (1-based; valid indices are [1 .. size]).
Notice that string constants (and freeze values) are immutable (i.e. readonly strings). Only strings which have been created dynamically can be modified.
  • ) this non-ANSI-Smalltalk syntax is supported by Smalltalk/X and thus also valid in expecco.

Copying[Bearbeiten]

aString copy
Generates a mutable copy.
Example:
 'hello world' copy at:1 
     => $h
aString copyFrom: startIndex to: endIndex
aString.copyFrom_to( startIndex, endIndex )   [JS]
Copies characters from a start index to an end index (1-based; valid indices are [1 .. size]).
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
     => '' (an empty string)

 '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]
the Smalltalk comma operator1 and the JavaScript plus operator concatenate two strings
Example (Smalltalk):
 'hello' , ' ' , 'world'
     => 'hello world'
Example (JavaScript):
 "hello" + " " + "world"
     => "hello world"
string ,* integer
the Smalltalk comma-star operator generates a new string by repeating the first string a number of times
Example:
 'abc' ,* 5
     => 'abcabcabcabcabc'

1)Although in theory, the plus operator could have been overloaded in Smalltalk as well, this was not done for readability and debuggability reasons: using a comma makes it clear that the intention is string concatenation and not addition. Also notice, that the JavaScript semantics can be considered unclean, as it concatenates strings if either operand is a string. Thus, you have to be careful if addition is attempted, and either incoming operand could possibly be a string. In contrast, Smalltalk will report an error if you try to concatenate or add strings and numbers.

Splitting[Bearbeiten]

aString splitBy: aCharacter
aString.splitBy( aCharacter ) [JS]
Splits a string into pieces, given a splitting character. The result is a collection containing the parts.
Examples:
 'hello world here are six words' splitBy: $      <- trailing invisible space here
     => #( 'hello' 'world' 'here' 'are' 'six' 'words')

 'hello world here are six words' splitBy: (Character space) <- no invisible space
     => #( '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: #<name of 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/the name of an element-method, which returns true to split.
This is a more general version of the above "splitBy:", for complex splits.
Examples:
 'hello here are five words' splitOn: $      <- trailing space here
     => #( 'hello' 'here' 'are' 'five' '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, commas and semis; here' splitOn: [:ch | (ch == $,) or:[ ch == $; ]]
     => #( 'hello' 'commas and semis' 'here')

 'aWordWithCamelCase' splitOn: [:ch | ch isUppercase ]
     => #( 'a' 'Word' 'With' 'Camel' 'Case')

 'aWordWithCamelCase' splitOn: #isUppercase
     => #( 'a' 'Word' 'With' 'Camel' 'Case')

 c'some text\twith\ndifferent separators' splitOn: #isSeparator
     => #( 'some' 'text' 'with' 'different' 'separators')

 '123abc456def789' splitOn:'[a-z]*' asRegex 
     => #('123' '456' '789')

JS examples:
 "hello here are five words".splitOn( $' ' )    <- space character here
     => [ "hello" , "here" , "are" , "five" , "words" ]

 "hello here are five words".splitOn( ' ' )    <- string with a space here
     => [ "hello" , "here" , "are" , "five" , "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.
Example:
 'HELLO' sameAs: 'HeLlo'
     => true

 'HELxO' sameAs: 'HeLlo'
     => false

 'HELLO' caselessEqual: 'HeLlo'
     => true
string1 caselessBefore: string2
string1 caselessAfter: string2
string1 caselessEqual: string2
string1.caselessBefore( string2 )   [JS]
string1.caselessAfter( string2 )   [JS]
Compare two strings, ignoring case.
Returns true if string1 is smaller, same or larger than string2.
Does not care for national characters (i.e. the compare is based on the unicode points)
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:
 '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). The index is 1-based; 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:
 '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
aString indexOfAny: 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:
 'HELLO, WORLD' indexOfAny: ',;'
     => 6

 'HELLO; WORLD' indexOfAny: ',;'
     => 6

 'HELLO; WORLD' indexOfAny: #( $, $; )
     => 6

 'HELLO. WORLD' indexOfAny: #( $, $; )
     => 0
aString indexOfSeparator
aString indexOfSeparatorStartingAt: startIndex
aString lastIndexOfSeparator
aString lastIndexOfSeparatorStartingAt: startIndex
Searches for the first/last whitespace character (Space, Tab, CR or NL).
Returns 0 if not found.
Example:
 'HELLO WORLD' indexOfSeparator
     => 6

 'HELLO WORLD' indexOfSeparatorStartingAt: 7
     => 0

 'HELLO abc World' lastIndexOfSeparator
     => 10

 'HELLO abc World' lastIndexOfSeparatorStartingAt:10
     => 10

 'HELLO abc World' lastIndexOfSeparatorStartingAt:9
     => 6
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
aString includesString: aSubstring [ caseSensitive: boolean ]
Returns true if aString contains the sub-string aSubstring; false otherwise.
Example:
 'HELLO' includesString:'LL'
     => true

 'HELLO' includesString: 'll'
     => false

 'HELLO' includesString: 'll' caseSensitive: false 
     => true

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
 ('myFile.txt' asFilename contents) select:[:line |
    line matchesRegex:'[0-9]+\:.*' 
 ]
     => all lines from the file which start with a number followed by a colon.
patternString regexMatchesIn: aString
Returns a collection containing all regex matching substrings.
Example
 '[0-9]+' regexMatchesIn: '1234 abcd 3456 defg'
     => OrderedCollection('1234' '3456')
aString subExpressionsInRegex: patternString caseSensitive: boolean
Returns a collection containing the individual partial matches. Partials are regex subexpressions in parentheses.
Example
 '1234abc3456' subExpressionsInRegex: '([0-9]+)abc([0-9]+)' caseSensitive:false
     => OrderedCollection('1234' '3456')

 '123 abc xyz :bla' subExpressionsInRegex: '([0-9]+).*(\:.*)' caseSensitive:false
     => OrderedCollection('123' 'bla')
Notice that ':' is a special character in the pattern and must therefore be escaped with a backslash.
patternStrings compoundMatch: aString [ caseSensitive: boolean ]
True if a string matches any pattern in patternStrings, which contains multiple GLOB match patterns separated by semicolon.
Useful eg. to verify a string against multiple file extensions (eg. '*.txt;*.doc;*.pdf' compoundMatch: aFilename).
Example:
 'f*;b*' compoundMatch:'foo'
     => true

 'f*;b*' compoundMatch:'bar'
     => true

 'f*;b*' compoundMatch:'xxx'
     => false

Converting[Bearbeiten]

aString asByteArray
Returns a byte array containing the codePoints (Unicode codes).
The characters should be within the ISO8859 range 0x00 .. 0xFF.
If they are not, a byte array containing the wide characters is returned (to be exact: in the machine's native byte order, which is low byte first on x86 cpus).
Example:
 'abc123' asByteArray
     => #[97 98 99 49 50 51]

 'äöü' asByteArray
     => #[228 246 252]

 c'abc\u2022123' asByteArray
     => #[97 0 98 0 99 0 34 32 49 0 50 0 51 0]
aString asFilename
Returns a filename instance, which provides functions to operate on files and directories.
See "Filename protocol" for its functions.
Example:
 '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' ... )
aString asCollectionOfWords
Returns a collection of strings, each containing one word from the original string. Words are separated by whitespace (space, tab or CR/LF).
Example:
 'hello bla bla world' asCollectionOfWords
     => #( 'hello' 'bla' 'bla' 'world' )

Encoding / Decoding[Bearbeiten]

aString utf8Encoded
aString utf8Decoded
Encode / decode into/from utf8 encoding.
Example:
 'äöü' utf8Encoded
     => 'äöü'

 'äöü' utf8Encoded asByteArray
     => #[195 164 195 182 195 188]

 'äöü' utf8Encoded utf8Decoded
     => 'äöü'
aString utf16Encoded
Encode into an utf16 encoded two byte string (ie. still a vector of characters), caring for characters above codepoint 0xFFFF. This can then be further converted to a vector of bytes in BE (big end) or LE (low end) order (see below).
Example:
 'hello äöü' utf16Encoded encodeInto:#utfLE16
     => #[104 0 101 0 108 0 108 0 111 0 32 0 228 0 246 0 252 0]
aString encodeInto: nameOfEncoding
aString decodeFrom: nameOfEncoding
Similar to the above, but nameOfEncoding gives the encoding's name (as symbol). Supported encodings are: #utf, #utf16BE, #utf16LE, #iso8859-1 to #iso8859-8, #iso8859-15, #iso8859-16, #koi7, #cp437, #cp850, #cp1250, #cp1252, #jis7, #gb, #big5, #ebcdic.
aString encodeFrom: nameOfEncoding1 into: nameOfEncoding2
to transcode from one encoding into another. E.g. "someString encodeFrom:#cp850 into:#koi7" would recode from the Windows codepage 850 into cyrillic koi7.
aString base64Encoded
aString base64Decoded
aString base64DecodedString
Encode / decode into/from base64 encoding.
Example:
 'äöü' base64Encoded
     => '5Pb8'

 'äöü' base64Encoded asByteArray
     => #[53 80 98 56]

 'äöü' base64Encoded base64Decoded
     => #[228 246 252]

 'äöü' base64Encoded base64DecodedString
     => 'äöü'

 'äöü' base64Encoded base64Decoded asString
     => 'äöü'

Hashing[Bearbeiten]

See Cryptographic API Functions.



Copyright © 2014-2024 eXept Software AG