Lecture#1 String Manipulation
Lecture#1 String Manipulation
String Manipulation
Introduction to strings
• One of the basic data types in programming is the 'string’.
• A string variable stores text rather than a number. The string is made up of a
set of characters that can also include spaces, symbols and numbers.
• Examples of strings:
"a“
"this is a string"
"0145955"
"this is a string with numbers 1223“
"*$&££^"((*££&&*$$$"
• Most programming languages have a number of handy functions to manipulate
strings.
String assignment
• The first step in using strings is to get the program to recognise which
variables are meant to be strings.
• Example:
• DECLARE MyLargeString : STRING (Declaration)
• MyLargeString = "Hello this is a string“ (Assignment)
String assignment (Cont..)
• Strings can have numbers inside of them, but the number is now
handled as just another set of characters.
• You can't carry out any calculations on numbers in a string without
first converting it into a different data type, like an integer or floating
point number(Real Numbers).
• For example a telephone number can be stored in this way as no
maths is likely to be carried out on it.
MyTelephoneNumber = "01926111111"
INPUT a String from a user
• Strings can be input by the user and stored in a string variable.
• For example the pseudocode :-
YourName = INPUT(‘’Please enter your first name‘’)
• This displays a prompt on the screen asking the user to enter a string.
When they do, it is stored as the YourName variable.
Storage
• Each character in a string is stored as a single byte. If there is more
than one character in the string, the group is given a name and handled
in the program as a single item.
• For example: MyLargeString = "Hello this is a string“
• MyLargeString is handled as one variable. It takes 22 bytes of memory
to store all of the characters in this string (remember that blank spaces
take up as much space as any other character).
String copy
• programs can be written to do various things to the text inside them.
You can add, remove, edit, or search the text using various
commands.
• use the assignment ‘' operator, like this :-
MyString SecondString
• This pseudocode copies the content of SecondString into
the MyString variable, replacing whatever was in MyString.
Built in Function
Length of a string(Returns the integer value representing the
length of string S)
• Pseudocode:
LENGTH(S : STRING)
RETURNS INTEGER
VB.NET
len(S)
Length of a string Example in Pseudocode
• Function:
X <-“Blue Jays School System”
A<- LENGTH(X)
• OUTPUT : 23
• TYPE: INTEGER
Length of a String in VB
Module Module1
Sub Main()
Dim x As String
Dim a As Integer
a = Len(x)
End Sub
End Module
Extraction(Left)
Sub Main()
Dim X As String
Dim Y As String
Y = Left(X, 4)
Console.WriteLine("Left(X, 5)= " & Y)
Console.ReadKey()
Y = Right(X, 6)
Console.WriteLine("Right(X, 4)= " & Y)
Console.ReadKey()
Y = Mid(X, 6, 4)
Console.WriteLine("Mid(X, 7, 3)= " & Y)
Console.ReadKey()
End Sub
End Module
Searching Pattern
• X <- “Blue Jays School System”
e= LOCATE(X,” ”) // 5
f= LOCATE(e+1,X,” “) // 10
g= LOCATE(X,”J”) // 6
Module Module1
Sub Main()
Dim X As String
Dim a, b, c As Integer
c = InStr(X, "J")
Console.WriteLine(c)
Console.ReadKey()
End Sub
End Module
Concatenation (join two strings)
End Sub
End Module
Char to ASCII
• Returns the ASCII value of character ch
Pseudo code:
ASC(ch)
RETURNS INTEGER
Example:
X <- ASC(‘ B ’) // 66
Sub Main()
'Char to ASCII
Dim ch As Char
Dim x As Integer
Console.WriteLine("Char to ASCII ")
Console.WriteLine("enter any character from A TO Z")
ch = Console.ReadLine
x = Asc(ch)
Console.WriteLine(x)
Console.ReadKey()
End Sub
End Module
ASCII TO CHAR
• Returns the character whose ASCII value is I
• Pseudo code:
CHR(i : INTEGER)
RETURNS CHAR
Example:
X <- CHR(66) // ‘B’
Sub Main()
'Char to ASCII
Dim ch As Char
Dim x As Integer
Console.WriteLine("ASCII TO CHAR ")
Console.WriteLine("enter any NUMBER FROM 1 TO 128")
x = Console.ReadLine
ch = Chr(x)
Console.WriteLine(ch)
Console.ReadKey()
End Sub
End Module
Char to ASCII and ASCII to Char in VB
Module Module1
Sub Main()
Dim N As Integer
Dim Y As Char
N = Asc("B")
Console.WriteLine(N)
Console.ReadKey()
Y = Chr(66)
Console.WriteLine(Y)
Console.ReadKey()
End Sub
End Module
Casting (Integer to String)
• NUM_TO_STR(x : <data type>) RETURNS STRING
• Example:
• X= NUM_TO_STR(34) // “34”
Casting (String to Number)
• STR_TO_NUM(x : <data type1>) RETURNS <data type2>
• returns a numeric representation of a string
• Note: <data type1> may be CHAR or STRING
• Note: <data type2> may be REAL or INTEGER
• Example:
• STR_TO_NUM("23.45") returns 23.45
Casting in VB
Module Module1
Sub Main()
Dim N As Integer = 0
Dim Y As String = ""
Y = Str(34)
Console.WriteLine("Str(34)= " & Y)
Console.ReadKey()
N = Val("567")
Console.WriteLine("Val(""567"")= " & N)
Console.ReadKey()
End Sub
End Module
Character into Upper Case
• UCASE(ThisChar : CHAR) RETURNS CHAR
x = LCase(b)
Console.WriteLine("lower case of FARAZ ALAM is: " & x)
Console.ReadKey()
End Sub
End Module
Changing case of string in VB(using
ToUpper and ToLower)
Module Module1
Sub Main()
Dim x, z, y As String
x = "Blue jays School system"
z = "Blue jays School system"
y = x.ToUpper
z = x.ToLower
End Sub
End Module