# Lesson 13 : String Manipulation Functions

### 13.1 The Len Function

The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is

Len (“Phrase”)

For example,

Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22

The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example,

X=sqr (16)

Y=1234

Z#=10#

Then Len(x)=1, Len(y)=4, and Len (z)=8

The reason why Len(z)=8 is because z# is a double precision number and so it is allocated more memory spaces.

### 13.2 The Right Function

The Right function extracts the right portion of a phrase. The syntax is

Right (“Phrase”, n)

Where n is the starting position from the right of the phrase where the portion of the phrase is going to be extracted.  For example,

Right(“Visual Basic”, 4) = asic

### 13.3 The Left Function

The Left\$ function extract the left portion of a phrase. The syntax is

Left(“Phrase”, n)

Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted.  For example,

Left (“Visual Basic”, 4) = Visu

### 13.4 The Ltrim Function

The Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is

Ltrim(“Phrase”)

.For example,

Ltrim (“  Visual Basic”, 4)= Visual basic

### 13.5 The Rtrim Function

The Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is

Rtrim(“Phrase”)

.For example,

Rtrim (“Visual Basic      ”, 4) = Visual basic

### 13.6 The Trim function

The Trim function trims the empty spaces on both side of the phrase. The syntax is

Trim(“Phrase”)

.For example,

Trim (“   Visual Basic      ”) = Visual basic

### 13.7 The Mid Function

The Mid function extracts a substring from the original phrase or string. It takes the following format:

Mid(phrase, position, n)

Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example,

Mid(“Visual Basic”, 3, 6) = ual Bas

### 13.8 The InStr function

The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is

Instr (n, original phase, embedded phrase)

Where n is the position where the Instr function will begin to look for the embedded phrase. For example

Instr(1, “Visual Basic”,” Basic”)=8

### 13.9 The Ucase and the Lcase functions

The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For example,

Ucase(“Visual Basic”) =VISUAL BASIC

Lcase(“Visual Basic”) =visual basic

### 13.10 The Str and Val functions

The Str is the function that converts a number to a string while the Val function converts a string to a number. The two functions are important when we need to perform mathematical operations.

### 13.11 The Chr and the Asc functions

The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for “American Standard Code for Information Interchange”. Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The syntax of the Chr function is

Chr(charcode)

and the syntax of the Asc function is

Asc(Character)

The following are some examples:

Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(“B”)=66, Asc(“&”)=38

We can create a Asc to Chr and Chr to Asc converter in the following Example:

In this example, we create two buttons, label one of them as ASC and the other one as CHR. Insert two textboxes, one for the user to enter an ASC code and the other one to enter the CHR character. When the user enter an ASC code, he or she can check for the corresponding character by clicking the CHR button. Likewise, he or she can check the corresponding ASC code after entering a character.

#### The Code

```            Private Sub CmdASC_Click()
TxtASC.Text = Asc(TxtCHR.Text)
End Sub

Private Sub CmdCHR_Click()
TxtCHR.Text = Chr(TxtASC.Text)
End Sub
```

### 13.12 The String function

The String function has two arguments, a number and a single-character string, and returns a string consisting of the specified character repeated the speficified number of times. The syntax of the String function is:

`    String(n,"Character")`

For example, String(30, "#") will return the # sign 30 times, as shown in the program below:

```
Form1.Show
Print String(30, "#")

End Sub
```

The output

### 13.13 The vbCrLf Named Constant

The vbCrLf named constant is a combination of two abbreviations Cr and Lf. Cr has numeric code Chr(13) which represents carriage return and Lf has numeric code Chr(10) which represents line feed. Carriage return means move the cursor to the left of the text field, and line feed means move down one row in the text field. By combining Cr and Lf, vbCrLf make it possible to display multiple lines in a text field such as in a message box, as shown in the following example.

```
Private Sub Command1_Click()
Dim message As String
Dim display As String

message = "Mission to Mars"
display = String(30, "*") & vbCrLf & message & vbCrLf & String(30, "*")
MsgBox display

End Sub```

The output

### 13.14 Performing Word Search

We can make use of various string functions to perform word search from a textbox.

In the following example, we insert a textbox and set the multiline property to true. We also insert a textbox for the user to enter the word to search and a command button to perform the search. Besides that, we also include a label control to display the result. In the code, we use the set setFocus property to highlight the word found. In addition, we also use the SelStart to set the starting point of text selected.

#### The Code

```Private Sub cmdSearch_Click()
Dim n, m, l As Integer
Dim myAricle, myWord As String
myArticle = TxtArticle.Text
myWord = TxtWord.Text
l = Len(myWord)
n = InStr(1, myArticle, myWord)
If n = 0 Then

Else
LblResult.Caption = "Found your word " & myWord & " at " & " Position " & n
TxtArticle.SetFocus
TxtArticle.SelStart = n - 1
TxtArticle.SelLength = Len(myWord)
End If
End Sub
```
The text passage is a detailed explanation of various string manipulation functions in Visual Basic, including examples and syntax for each function.
no_question

the passage does not contain any mathematical questions or problems that require solving. it is purely instructional, providing information and examples on how to use string manipulation functions in visual basic.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Lesson 13 : String Manipulation Functions

### 13.1 The Len Function

The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is

Len (“Phrase”)

For example,

Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22

The Len function can also return the number of digits or memory locations of a number that is stored in the computer. For example,

X=sqr (16)

Y=1234

Z#=10#

Then Len(x)=1, Len(y)=4, and Len (z)=8

The reason why Len(z)=8 is because z# is a double precision number and so it is allocated more memory spaces.

### 13.2 The Right Function

The Right function extracts the right portion of a phrase. The syntax is

Right (“Phrase”, n)

Where n is the starting position from the right of the phrase where the portion of the phrase is going to be extracted.  For example,

Right(“Visual Basic”, 4) = asic

### 13.3 The Left Function

The Left\$ function extract the left portion of a phrase. The syntax is

Left(“Phrase”, n)

Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted.  For example,

Left (“Visual Basic”, 4) = Visu

### 13.4 The Ltrim Function

The Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is

Ltrim(“Phrase”)

.For example,

Ltrim (“  Visual Basic”, 4)= Visual basic

### 13.5 The Rtrim Function

The Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is

Rtrim(“Phrase”)

.For example,

Rtrim (“Visual Basic      ”, 4) = Visual basic

### 13.6 The Trim function

The Trim function trims the empty spaces on both side of the phrase. The syntax is

Trim(“Phrase”)

.For example,

Trim (“   Visual Basic      ”) = Visual basic

### 13.7 The Mid Function

The Mid function extracts a substring from the original phrase or string. It takes the following format:

Mid(phrase, position, n)

Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example,

Mid(“Visual Basic”, 3, 6) = ual Bas

### 13.8 The InStr function

The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is

Instr (n, original phase, embedded phrase)

Where n is the position where the Instr function will begin to look for the embedded phrase. For example

Instr(1, “Visual Basic”,” Basic”)=8

### 13.9 The Ucase and the Lcase functions

The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For example,

Ucase(“Visual Basic”) =VISUAL BASIC

Lcase(“Visual Basic”) =visual basic

### 13.10 The Str and Val functions

The Str is the function that converts a number to a string while the Val function converts a string to a number. The two functions are important when we need to perform mathematical operations.

### 13.11 The Chr and the Asc functions

The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for “American Standard Code for Information Interchange”. Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The syntax of the Chr function is

Chr(charcode)

and the syntax of the Asc function is

Asc(Character)

The following are some examples:

Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(“B”)=66, Asc(“&”)=38

We can create a Asc to Chr and Chr to Asc converter in the following Example:

In this example, we create two buttons, label one of them as ASC and the other one as CHR. Insert two textboxes, one for the user to enter an ASC code and the other one to enter the CHR character. When the user enter an ASC code, he or she can check for the corresponding character by clicking the CHR button. Likewise, he or she can check the corresponding ASC code after entering a character.

#### The Code

```            Private Sub CmdASC_Click()
TxtASC.Text = Asc(TxtCHR.Text)
End Sub

Private Sub CmdCHR_Click()
TxtCHR.Text = Chr(TxtASC.Text)
End Sub
```

### 13.12 The String function

The String function has two arguments, a number and a single-character string, and returns a string consisting of the specified character repeated the speficified number of times. The syntax of the String function is:

`    String(n,"Character")`

For example, String(30, "#") will return the # sign 30 times, as shown in the program below:

```
Form1.Show
Print String(30, "#")

End Sub
```

The output

### 13.13 The vbCrLf Named Constant

The vbCrLf named constant is a combination of two abbreviations Cr and Lf. Cr has numeric code Chr(13) which represents carriage return and Lf has numeric code Chr(10) which represents line feed. Carriage return means move the cursor to the left of the text field, and line feed means move down one row in the text field. By combining Cr and Lf, vbCrLf make it possible to display multiple lines in a text field such as in a message box, as shown in the following example.

```
Private Sub Command1_Click()
Dim message As String
Dim display As String

message = "Mission to Mars"
display = String(30, "*") & vbCrLf & message & vbCrLf & String(30, "*")
MsgBox display

End Sub```

The output

### 13.14 Performing Word Search

We can make use of various string functions to perform word search from a textbox.

In the following example, we insert a textbox and set the multiline property to true. We also insert a textbox for the user to enter the word to search and a command button to perform the search. Besides that, we also include a label control to display the result. In the code, we use the set setFocus property to highlight the word found. In addition, we also use the SelStart to set the starting point of text selected.

#### The Code

```Private Sub cmdSearch_Click()
Dim n, m, l As Integer
Dim myAricle, myWord As String
myArticle = TxtArticle.Text
myWord = TxtWord.Text
l = Len(myWord)
n = InStr(1, myArticle, myWord)
If n = 0 Then

Else
LblResult.Caption = "Found your word " & myWord & " at " & " Position " & n
TxtArticle.SetFocus
TxtArticle.SelStart = n - 1
TxtArticle.SelLength = Len(myWord)
End If
End Sub
```
user
The text passage is a detailed explanation of various string manipulation functions in Visual Basic, including examples and syntax for each function.

TYPE: NO_QUESTION

The passage does not contain any mathematical questions or problems that require solving. It is purely instructional, providing information and examples on how to use string manipulation functions in Visual Basic.