0% found this document useful (0 votes)
4 views21 pages

Visual Basic String Manipulation Guide

The document provides an overview of string manipulation techniques in Visual Basic, including functions like Len, Str, capitalize, lower, count, endswith, find, islower, isupper, strip, replace, split, swapcase, and title. Each function is illustrated with examples that demonstrate its usage and output. Additional string methods are also listed, highlighting their purposes.

Uploaded by

mariah jabesi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Visual Basic String Manipulation Guide

The document provides an overview of string manipulation techniques in Visual Basic, including functions like Len, Str, capitalize, lower, count, endswith, find, islower, isupper, strip, replace, split, swapcase, and title. Each function is illustrated with examples that demonstrate its usage and output. Additional string methods are also listed, highlighting their purposes.

Uploaded by

mariah jabesi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

STRING MANIPULATION

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 1 Starting Out with Visual Basic 3rd Edition
Len Function
• Displays the length of the string
string = “Awesome Python”
print (len(string))

Output:
14

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 2 Starting Out with Visual Basic 3rd Edition
Str Function
• Converts a number to a string
string = “I am ”
age = 30
print (string + str(age)) or print (string + ‘age’)

Output:
I am 30

NB: str or ‘ ‘ convert value 18 to string.


manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 3 Starting Out with Visual Basic 3rd Edition
Example 1
string = “Awesome Python”
print (string(0:6))

Output:
Awesome

NB: 0 indicates the first character A and 6 is


the seventh character e.

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 4 Starting Out with Visual Basic 3rd Edition
[Link]
• Capitalize the first letter in a string
string = “i am feeling good.”
print ([Link]())

Output:
I am feeling good.

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 5 Starting Out with Visual Basic 3rd Edition
[Link]
• Capitalize the first letter in a string
string = “ENJOY PYTHON.”
print ([Link]())

Output:
enjoy python.

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 6 Starting Out with Visual Basic 3rd Edition
[Link]
• Count the number of occurrence of a given
word in a string
string = “i am feeling good very goog.”
print ([Link](‘good’))

Output:
2

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 7 Starting Out with Visual Basic 3rd Edition
[Link]
• Checks whether a string ends with a given
word
string = “ [Link]”
print ([Link](‘.zw’))

Output:
True

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 8 Starting Out with Visual Basic 3rd Edition
[Link]
• Finds the position of a given word in a string
string = “I enjoy computers.”
print ([Link](‘enjoy’))
Output:
2 :Exists at position 2. Starts counting at 0

string = “I enjoy computers.”


print ([Link](‘you’))
Output:
-1 :You does not exist in the string
manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 9 Starting Out with Visual Basic 3rd Edition
[Link]
• Finds whether the string is lower case

string = “I Enjoy Computers.”


print ([Link]())

Output:
False : I, E and C are all uppercase.

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 10 Starting Out with Visual Basic 3rd Edition
[Link]
• Finds whether the string is upper case

string = “ENJOY PYTHON.”


print ([Link]())

Output:
True

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 11 Starting Out with Visual Basic 3rd Edition
[Link]
• Removes certain given characters in a string

string = “!!!!Hie! How are you!!!!?”


print ([Link](‘!’))

Output:
“Hie How are you?”

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 12 Starting Out with Visual Basic 3rd Edition
[Link]
• Removes certain left characters in a string

string = “ !!!!!How are you?”


print ([Link](‘!’))

Output:
“How are you?”

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 13 Starting Out with Visual Basic 3rd Edition
[Link]
• Removes certain right characters in a string

string = “Hie! How are you!!!!?”


print ([Link](‘!’))

Output:
“Hie! How are you?”

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 14 Starting Out with Visual Basic 3rd Edition
[Link]
• Replaces certain word(s) in a string

string = “Hie! How are you!!!!?”


print ([Link] (‘Hie’, ‘Hello’))

NB: Replaces Hie with Hello

Output:
“Hello! How are you?”
manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 15 Starting Out with Visual Basic 3rd Edition
[Link]
• Separates a string into two or three
depending on whether there are spaces
between words.

string = “Lovemore Manyeruke?”


print [Link]()

Output:
Lovemore Manyeruke
manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 16 Starting Out with Visual Basic 3rd Edition
[Link] cont’d
• Also used to remove certain parts of an original
string.
• For example, let’s remove the letter a from the
string:
balloon = "Sammy has a balloon.”
print [Link](‘a’)

Output:
'S', 'mmy h', 's ', ' b', 'lloon.'

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 17 Starting Out with Visual Basic 3rd Edition
[Link]
• Changes lower to uppercase and uppercase
to lowercase.

string = “I love PROGRAMMING”


print [Link]()

Output:
i LOVE programming

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 18 Starting Out with Visual Basic 3rd Edition
[Link]
• Changes text in a string to title case.

string = “I love PROGRAMMING”


print [Link]()

Output:
I Love Programming

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 19 Starting Out with Visual Basic 3rd Edition
Other String Methods
• [Link]()
• [Link]
• [Link]
• [Link]
• [Link] (is alphanumeric)
• [Link] (is alphabetic)
• [Link]

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 20 Starting Out with Visual Basic 3rd Edition
Example
number = 5
letters = "abcdef“
print([Link]())
print([Link]())

Output
True
False

manyerukel@[Link] 0777634287/0712389336
Chapter 3, Slide 21 Starting Out with Visual Basic 3rd Edition

Common questions

Powered by AI

The strip() method removes specified characters from both ends of a string, like '!!!!Hie! How are you!!!!?' becoming 'Hie! How are you?'. The lstrip() removes characters from the left end, and rstrip() from the right end only, such as ' !!!!!How are you?' becoming 'How are you?' using lstrip('!').

The replace() method updates specific substrings with new content, which is crucial for modifying string data efficiently. For example, altering 'Hie! How are you!!!!?' to 'Hello! How are you?' involves replace('Hie', 'Hello'), crucial for data normalization and correction tasks .

The capitalize() method converts the first character of a string to uppercase and changes the rest to lowercase, as seen in converting 'i am feeling good.' to 'I am feeling good.' The lower() method converts all characters in a string to lowercase, as demonstrated by changing 'ENJOY PYTHON.' to 'enjoy python.' .

The count() function identifies the frequency of a specified substring within a larger string. For instance, in 'i am feeling good very good,' string.count('good') returns 2, indicating 'good' appears twice. The endswith() function checks if a string concludes with a specified substring, such as '.zw' in ' www.mutareteachers.ac.zw', returning True if it does .

The str() function is used to convert numeric data types into strings, allowing for concatenation with other string data. For example, when a user has an integer like age=30, it can be converted to a string using str(age) and concatenated with 'I am ' to form 'I am 30' .

The find() method searches for a specified substring within a string and returns the starting index position or -1 if not found. In 'I enjoy computers.', find('enjoy') returns 2, while find('you') returns -1. In contrast, islower() and isupper() are boolean methods assessing if all characters in a string are lowercase or uppercase, respectively. For example, 'I Enjoy Computers.' islower() returns False, while 'ENJOY PYTHON.' isupper() returns True .

The len() function calculates the number of characters in a string. For instance, in the string 'Awesome Python', len(string) returns 14, representing the total count of characters, including spaces .

String slicing is demonstrated with the example string 'Awesome Python', using string(0:6) which outputs 'Awesome'. The method extracts a substring from index 0 up to, but not including, index 6, as indicated by the slicing notation .

The swapcase() method changes the case of each character in a string, aiding text normalization where case consistency is required. For example, 'I love PROGRAMMING' becoming 'i LOVE programming'. The title() method capitalizes the first letter of each word, ideal for formatting titles or headings, transforming 'I love PROGRAMMING' to 'I Love Programming' .

The split() method divides a string into a list based on a delimiter. When no delimiter is specified, it splits at spaces, such as 'Lovemore Manyeruke?' becoming ['Lovemore', 'Manyeruke']. When a specific character like 'a' is used in 'Sammy has a balloon.', split('a') produces ['S', 'mmy h', 's ', ' b', 'lloon.'], useful for parsing structured text where data is character-separated .

You might also like