6
Strings
Strings are an
indispensable part of any programming language. They are used to input,
manipulate and output a collection of characters or words. Since, every program
has to deal with a large amount of textual data, the significance of string
handling cannot be over-emphasised. The importance of WMLScript will be
highlighted in this chapter in the context of using strings. The actual intent
behind having WML and WMLScript is to allow a user to enter data, modify it and
output the result. The data may be in the form of numbers or characters. Hence,
as far as a collection of characters is concerned, a series of functions are
mandatory to facilitate string manipulations.
In fact, languages like
Perl and ASP are preferred by programmers who need to carry out a large
number of string manipulations because
of the diverse range of number functions that they provide for this purpose. Similarly,
WMLScript has a large number of functions that deal exclusively with Strings.
WMLScript has a library called String, which contains all the functions that
deal with strings. We have already familiarized ourselves with the length
function.
The first function that we
shall look at is isEmpty.
|
Screen 6. 1 |
aaa.wmls
extern function abc()
{
var i;
i = String.isEmpty("");
if ( i )
Dialogs.alert("true");
else
Dialogs.alert("false");
}
A string is considered
empty if it consists of two double inverted commas with no space between them.
If you pass this value to the isEmpty function, it will return true. If you put
any text such as "abc" or even a space between the double inverted
commas, the function isEmpty will return false.
|
Screen 6. 2 |
aaa.wmls
extern function abc()
{
var i;
i = String.isEmpty(" ");
if ( i )
Dialogs.alert("true");
else
Dialogs.alert("false");
}
Remember, all string
manipulations require numbers or integers. If you enter a number with a
floating point value, Float.int is used
to remove the decimal place.
We require a function that
gives us the position of a specified character within a string. There is a
function called charAt which is used
for this purpose. In the illustration given below, we have asked the function
to convey to us as to which character is available at position 1 in the string
"ABC".
|
Screen 6. 3 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.charAt("ABC",1);
Dialogs.alert(aa);
}
The length of the string is
3, but since we start counting from 0, the character at position 1 refers to
the second character of the string, which happens to be B. The function charAt returns a string of
length 1. When you deal with strings, you can have a string consisting of just
one character. Such a string will not have a datatype of character.
If you ask the fuction charAt to return the character at position
20, it will return an empty string ,since the length of the string is only 3.
Therefore, in the following example, where the function charAt has been passed
a value of 5 and the string has only 3 characters, the result will be an empty
string.
|
Screen 6. 4 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.charAt("ABC",5);
Dialogs.alert(aa);
}
If you pass a value such as
-1 to this function, the return value will again be an empty string.
Now, we need to go a step
further and look at another function that will return a part of the string that
is given as a parameter. This function is called substring. The first parameter
to this function is a string; the second one gives the position from where the
substring is to be extracted; and the third parameter specifies the number of
characters to be extracted. Here also, the function starts counting from 0. The
number 2 means that the extraction should start from the third character i.e. C
in this example; and the number 3 specifies that 3 characters are to be
extracted.
|
Screen 6. 5 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.subString("ABCDEF",2,3);
Dialogs.alert(aa);
}
The value that is returned
is "CDE ", which is a substring.
· If the start index which
is the second parameter, is less than 0, WMLScript will first error check and
make it 0.
· If the start index is
more than the length of the string, the function will return a null string or
an empty string.
· If the number of
characters to be extracted is more than the number of characters available for
extraction, this number will get replaced by the length of the string.
The next function has
been repeated from the third chapter
which deals with variables. We have already stated that integers, floats and boolean values get converted to a string.
WMLScript does this internally by using the toString function.
|
Screen 6. 6 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.toString(100);
Dialogs.alert(aa);
}
If we input the number 100 to
toSring, it will return a string "100". However, if we input the
number 100.67, it will return a string "100.67". The Boolean values
of true and false also get converted to strings, as explained in Chapter 3.
Thus, we could have used the function toString instead of using the function
format. The functions String.format and String.toString are synonymous with
each other and can be used interchangeably.
The only difference here is
that if you input an invalid value, toString will return the string "invalid". This is the only function that converts the
invalid value to a string. And, this happens to be the only variation from
WMLScript. It is pertinent to note that "invalid" is a separate data
type by itself, and you cannot convert it to anything else.
There is another function
called compare.
|
Screen 6. 7 |
aaa.wmls
extern function abc()
{
var i,aa;
i = String.compare("ABC","PQR");
aa = String.format("%d..",i);
Dialogs.alert(aa);
}
The function compare takes
two strings as parameters. The output can be as follows :
· If the two strings are
the same, then the function compare returns 0.
· If the first string is
larger than the second, it returns a value of +1.
· If the first string is
smaller (which is the case here), it will return a value of -1.
For two strings to be
considered equal, they have to contain the same number of characters and also
the same characters. So, ABCD and ABC are not equal because one has a length of
4 and the other has a length of 3. This function does not carry out case
folding. Case folding simply means conversion from the lower case to the upper
case, and vice versa. So, this function
is case sensitive. Thus, the strings "ABC" and "abc" are
not equal.
As a programmer, you will
need a provision, by means of which, you can remove spaces from a string. For
this purpose, a function called squeeze has been provided.
|
Screen 6. 8 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.squeeze(" ABC DEF ");
Dialogs.alert(aa);
}
Squeeze removes all the spaces
in the string, whether they are at the beginning, in the middle, or at the
end of the string. There is another
function called trim that removes the leading and trailing spaces.
|
Screen 6. 9 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.trim(" ABC DEF ");
Dialogs.alert(aa);
}
Next, we shall look at the function
String.find that lets you search for a substring within a string.
|
Screen 6. 10 |
aaa.wmls
extern function abc()
{
var aa,i;
i = String.find("ABCDEF","CD");
aa = String.format("%d..",i);
Dialogs.alert(aa);
}
For example, if you search
for the substring "CD" in the string "ABCDEF", the return
value of the function will be 2, as the
function will find it at the third location. Remember, the function starts
counting from 0.
If you change CD to CDF,
you will get -1 as the answer because this substring is not found in the
string. The function find does not look for a part of the substring. It either
finds the whole substring or none at all.
The next function is
String.replace that enables you to replace one substring with another within a
string.
|
Screen 6. 11 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.replace("ABCD AB AD ","AB","XYZ");
Dialogs.alert(aa);
}
In the original string, we
wish to find a string called AB and replace it with XYZ. First, the substring
AB which is part of ABCD gets replaced with XYZ. Then, the next occurrence of
AB gets replaced by XYZ. But, the substring AD does not get replaced because it
does not match the substring AB. All
the occurrences of the substring get replaced within the main string.
The function explored below
is String.elements.
|
Screen 6. 12 |
aaa.wmls
extern function abc()
{
var i,aa;
i = String.elements("hi,bye,",",");
aa = String.format("%d..",i);
Dialogs.alert(aa);
}
Now, we shall cover an
interesting function called String.Elements. WMLScript has a concept of arrays.
The second symbol that we pass as a parameter is a separator, which is a single
character. In the example above, it is a comma. The string has 3 components :
The first component is upto
the first comma.
The second one is the text
bye.
The third one is a null.
Since the string has 3
elements, the function string.elements will return a value of 3.
If there are 2 commas and
the 3rd element is null, string.element will still return 3. Now, suppose we
had used a comma and a semicolon as separators, it is smart enough to recognize
only the first one, which is a comma. So, do not put multiple separators
because they will all get ignored and only the first one will be accepted.
An empty string is also
valid. The function string.element can never return a value of 0 or a negative
value. It will return a value of 1 even for an empty string ,since an empty string
is also considered a separator.
If the first parameter is
an empty string and the second parameter is a comma, you will get an empty
string. If both are null, you will still get the value of 1, because an empty
string is a valid element.
So, we have the function
string.elementAt at this point.
|
Screen 6. 13 |
aaa.wmls
extern function abc()
{
var aa;
aa = String.elementAt("My name is vijay",1," ");
Dialogs.alert(aa);
}
The above example is
interesting. It is because there
are 4 elements and the separator is a
space. We have specified the second parameter of the function String.elementAt
as 1. This represents the second element of the string, which is
"name". Had we given a value of 0, the answer would have been
"My".
There are few cardinal
rules that you need to know :
· The index of the first
element is 0.
· If you give an index that
is larger than the actual number of elements, the function will return the last
element.
· If the first parameter is
an empty string, the function will return an empty string.
· If you give a separator
like a comma, which is not present in the string, it will return the entire
string.
The next function is
string.RemoveAt. It removes the element at a specified position in the string.
|
Screen 6. 14 |
aaa.wmls
extern function abc()
{
var i,aa;
aa = String.removeAt("My name is Vijay",1," ");
Dialogs.alert(aa);
}
Even the separator, which
immediately follows the specified element, is removed by this function. So, in
the above example, the element number 1 is "name". When the removeAt
function executes, it removes the element "name" and the space that
immediately follows this element. The string now becomes "My is
Vijay". All the cardinal rules
given above are applicable here also.
We shall now consider the
function String.replaceAt.
|
Screen 6. 15 |
aaa.wmls
extern function abc()
{
var i,aa;
aa = String.replaceAt("My name is Vijay","boss",1,"");
Dialogs.alert(aa);
}
ReplaceAt simply removes
the element at the specified position and replaces it with the word specified
as the second parameter. Here, we are interested in the second element. The
separator is a space, and the new word to be inserted is "boss". This
function will replace the element "name" with "boss", and
the resultant string will now become "My boss is Vijay". The same
cardinal rules are applicable here also.
The next function of
interest is String.InsertAt.
|
Screen 6. 16 |
aaa.wmls
extern function abc()
{
var i,aa;
aa = String.insertAt("My name is Vijay","boss",1,"");
Dialogs.alert(aa);
}
You would have noticed that
the parameters in this case, are similar in structure to the replaceAt
function. In this example, the function
adds the word "boss" as the second element of the string. The
resultant string now becomes "My boss name is Vijay". If the value of
1 is replaced with 7, and, since the string does not have 7 elements, the word
"boss" gets added after the last word. So, the resultant string now
becomes "My name is Vijay boss".
We have already used the
function String.format. This function is very similar to the printf function in
the C programming language. A format specifier of "%9.8d",10.6
will show the number 10 within 9 spaces/zeroes, and the decimal number 6
will be within 8 spaces/zeroes . The precision rules remain the same. Within
the double quotes, you can use either of the following :
· d for integer values
· f for decimal values
· s for character strings .
|
Screen 6. 17 |
aaa.wmls
extern function abc()
{
var i,aa;
i=10.6;
aa = String.format("%2.2f",i);
Dialogs.alert(aa);
}
|
Screen 6. 18 |
aaa.wmls
extern function abc()
{
var i,aa;
i="hello";
aa = String.format("%s",i);
Dialogs.alert(aa);
}