QUICK REFERENCE: CLASS STRING

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

size

Returns the quantity of digits of the Script.

Example

| a |
a:='abcdef'.
Transcript show:  a size.

Result

6

withCRs

Each symbol: \ is treated like a "carriage return".

Example

| a |
a:='ab\cde\f'.
Transcript show:  a withCRs.
 

Result

ab
cde
f

at:n

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

findString:'x'

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

findString:'x' startingAt: n caseSensitive: true|false

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

findBetweenSubStrs: 's'

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

beginsWith: 'x'

Is a test that returns true if the String begins with x.

Example

| a |
a:='abcdef'.
Transcript show:   ( a beginsWith:'d').

Result

false

caseInsensitiveLessOrEqual: 'x'

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

asLowercase

Transform all the characters of the String to lower case.

Example

| a |
a :='ABCdef'.
Transcript show: a asLowercase.

Result

abcdef

capitalized

Transform the first characters of the String to upper case.

Example

| a |
a:='abcdef'.
Transcript show:     a capitalized .

Result

Abcdef

surroundedBySingleQuotes

The String is surrounded by single quotes.

Example

| a |
a:='abcdef'.
Transcript show:    a  surroundedBySingleQuotes .

Result

'abcdef'

withSeparatorsCompacted

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'

replaceFrom: n1 to: n2 with: 'x' startingAt: n3

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

withNoLineLongerThan: n

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.

copyReplaceAll: 'x' with:'y'

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

DISCLAIMER: This material can be translated for any language, and reproduced total or partially by anybody using any type of media. But, please, don't put your name like author. And let me know if it was useful (americo@dmu.com).The use of any code, 3D model or technique published is free and doesn't need to have any reference about this source.The author is not responsible for any damage that the material can cause to your professional or sexual life.