
Smalltalk has a set of Classes very useful. At the next 4 lessons we will see a resume of the methods of the more importants. At this lesson and the next we will learn about two Classes of a group of Classes that has, the group, a name: collections. We will see details of two collections: String and Ordered Collection. We will not present all the methods of each Class but only that who we imagine are more used.
For each method we will present a mini-exercice. You need, inside the Squeak-Croquet environment, to open a new morphic project and the "green window". Drag two Tools: the old Workspace and the Transcript window.
You can copy and paste an exercice to the Workspace and "accept" it. When you "do-it"ize it, the result will appear at the Transcript window.
These "Quik References" lessons are very useful for consult during the programming time. You can print them for quick consult.
To create an object of the Class String you only need to assign a group of letters to a variable. Something like:
|a|
a := 'abcdef'.
and we have the String object: a.
The String object can call many methods. We will present first an useful operator. You need to have a good vision to see it - in magenta color):
,
The comma is an operator to concatenate two Strings.
Example
| a | a:='abcdef'. Transcript show: a,'xxx'
Result
abcdefxxx
Returns the quantity of digits of the Script.
Example
| a | a:='abcdef'. Transcript show: a size.
Result
6
Each symbol: \ is treated like a "carriage return".
Example
| a | a:='ab\cde\f'. Transcript show: a withCRs.
Result
ab cde f
Returns the character in the position n.
OBSERVATION: Pay attention to see how we write a method having a parameter been a parameter to another. Using parenthesis. The action inside the parenthesis runs first.
Example
| a | a:='abcdef'. Transcript show:(a at: 2) .
Result
b
Returns the position of the first character of the substring x.
IMPORTANT: The first position is 1 (not ZERO like in some languages).
Example
| a | a:='abcxxdbc'. Transcript show: ( a findString: 'bc') .
Result
2
Returns the position of the first character of the substring x starting from some position and considering the "casesensitiveness" (oops!).
Example
| a | a:='abcxxdBC'. Transcript show: ( a findString: 'BC 'startingAt: 1 caseSensitive: true) .
Result
0
OBSERVATION: The result was ZERO because we did a proposital mistake and the string is 'BC ' (having a blank) and we don't have this group of letters in the object. Change to: 'BC' and redo.
asUppercase
Change all the letters to uppercase.
Example
| a | a:='abcdef'. Transcript show: a asUppercase.
Result
ABCDEF
This method is very important! It's called "split" in some other languages. We use a "separator" s inside a group of substrings and this method splits the total string in many parts puting this parts like elements of an orderedColletion. We will talk about the orderedColletion in the next lesson. This Class has the method at: to pick the substring of some index. Look and play with the example to understand better.
TIP: We use this "trick" to desconstruct messages sended like Strings by the internet, from one computer to another.
Example
| a ordCol | a:='ab@cd@ef'. ordCol:= a findBetweenSubStrs: '@'. "returns orderedCollection" Transcript show: (ordCol at: 2) .
Result
cd
Is a test that returns true if the String begins with x.
Example
| a | a:='abcdef'. Transcript show: ( a beginsWith:'d').
Result
false
Another test that returns true if the String has the same characters (case insensitive) than the string x.
Example
| a | a:='abcdef'. Transcript show: ( a caseInsensitiveLessOrEqual:'aBCdEf').
Result
true
Transform all the characters of the String to lower case.
Example
| a | a :='ABCdef'. Transcript show: a asLowercase.
Result
abcdef
Transform the first characters of the String to upper case.
Example
| a | a:='abcdef'. Transcript show: a capitalized .
Result
Abcdef
The String is surrounded by single quotes.
Example
| a | a:='abcdef'. Transcript show: a surroundedBySingleQuotes .
Result
'abcdef'
If we have more than one "blank" between characters of the String this interval goes to have only one.
Example
| a | a:='ab cde f'. Transcript show: a withSeparatorsCompacted .
Result
'ab cde f'
Very important method! We can replace one digit or more at any place. Copy and paste the example at the Workspace for test.
Example
| a | a:='abcdef'. Transcript show: (a replaceFrom: 2 to: 2 with: 'X' startingAt: 1)
Result
aXcdef
Returns a String rewrapped so that no line has more characters than the given number.Copy and paste the example at the Workspace for test.
Example
| a | a:='This example is very simple.'. Transcript show: ( a withNoLineLongerThan:10)
Result
This example is very simple.
Replace all the 'x' by 'y'
Example
|a| a:='abcdef'. Transcript show: ( a copyReplaceAll: 'c' with:'X').
Result
abXdef
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
