String functions

Syntax is: %string options
Options are:

1. To return index of first match:
Syntax: string -first str1 str2
Searches first match of str1 in str2 and returns index of first character matched.

Example: 
%string first cat "The cat in the cat show"
4

2. To return index of last match:
Syntax: string -last str1 str2 

Searches last match of str1 in str2 and returns index of first character matched.

Example: 
%string last cat "The cat in the cat show"
15

3. To return a character at specified index

Syntax: string index str indx1
Returns character at specified indx1 in str

Example:
% string index "Hi! I am Ankit" 1
i

4. Returns a copy of a string with all alphabetical characters translated to its upper case

Syntax: string toupper string

Example:
puts [ string toupper "Hi! I am Ankit"] 
HI! I AM ANKIT

5. Returns a copy of a string with all alphabetical characters translated to its lower case

Syntax: string tolower string

Example:
puts [ string tolower "Hi! I am Ankit"] 
hi! i am ankit

6. Returns a copy of a string with all alphabetical characters translated to its title case

Syntax: string totitle string

Example:
puts [ string totitle "Hi! I am Ankit"] 
Hi! I Am Ankit

7. To replace all characters in between specified indexes with the specified characters

Syntax: string replace indx1 indx2 string replaceChar

Example:
% puts [string replace 8 10 "This is the dog" a]
This is a dog

8. To return the length of a string

Syntax: string length str

Example:
% puts [ string length "Ankit"]
5

9. To return if 2 strings are equal

Syntax: string [ -index ] [ -length num ] str1 str2
If -length option is not specified, entire string is compare, else only first num characters are compared. Does a character by character comparison between 2 strings.

Example:
% puts [ string "Ankit" "Ankit"]
1

10. To perform a lexicographic comparison among 2 strings

Syntax: string compare [ -length num ] str1 str2
Returns -1, 0, 1 if str1 is less than, equal to, or greater than str2.

Example: 
% puts [ string compare "Virat" "Dhoni" ]
1

11. To check if a pattern matches entire string

Syntax: string match pattern str
Returns 1 if matches, else 0. pattern is glob pattern.

Example:
% puts [ string match "*Dhoni*" "MS Dhoni is the best finisher in cricket"]
1











Comments

Popular posts from this blog

What should you expect in an VLSI interview as a fresher

The struggle to enter VLSI industry is for real