0% found this document useful (0 votes)
27 views

Lecture#1 String Manipulation

This document discusses various string manipulation functions in programming languages. It covers assigning and inputting strings, determining string length, extracting parts of strings, searching strings, concatenating strings, converting between character and ASCII codes, and changing the case of strings. Functions covered include length, left, right, mid, locate, concatenation with &, asc, chr, ucase, lcase, to_upper, and to_lower. Examples are provided in pseudocode and VB.NET.

Uploaded by

koimin203
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture#1 String Manipulation

This document discusses various string manipulation functions in programming languages. It covers assigning and inputting strings, determining string length, extracting parts of strings, searching strings, concatenating strings, converting between character and ASCII codes, and changing the case of strings. Functions covered include length, left, right, mid, locate, concatenation with &, asc, chr, ucase, lcase, to_upper, and to_lower. Examples are provided in pseudocode and VB.NET.

Uploaded by

koimin203
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture#1

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

x = "Blue Jays School System"

a = Len(x)

Console.WriteLine("length of the string is " & a)


Console.ReadKey()

End Sub

End Module
Extraction(Left)

• Returns leftmost L characters from S.


• Pseudocode
LEFT(S : STRING,L : INTEGER)
RETURNS STRING
• VB.NET
Left(S, L)
Extraction (right)
• Returns rightmost L characters from S.
• Pseudocode
RIGHT(S: STRING, L : INTEGER)
RETURNS STRING
• VB.NET
Right(S, L)
Extraction (mid)
• Returns a string of length L starting at position P from S
• Pseudocode
MID(S : STRING,P : INTEGER, L : INTEGER)
RETURNS STRING
• VB.NET
mid(S, P, L)
Extraction example in Pseudocode
X =“Blue Jays School System”
• FUNTION
B <- LEFT(X,4) // “Blue”
C <- RIGHT(X,6) // “System”
D <- MID(X,6,4) // “Jays”
• Data Type: String
Extraction in VB
Module Module1

Sub Main()
Dim X As String
Dim Y As String

X = "Blue Jays School System“

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

Data type: INTEGER


Searching Pattern in VB

Module Module1

Sub Main()
Dim X As String
Dim a, b, c As Integer

X = "Blue Jays School System"


a = InStr(X, " ")
Console.WriteLine(a)
Console.ReadKey()

b = InStr(a + 1, X, " ")


Console.WriteLine(b)
Console.ReadKey()

c = InStr(X, "J")
Console.WriteLine(c)
Console.ReadKey()

End Sub
End Module
Concatenation (join two strings)

• Concatenation means to link things together. You can do this with


strings using the ‘&' operator.
• Like this :-
Line 1: FirstName = input("Enter your first name")
Line 2: Surname = input("Enter your surname")
Line 3: Message = "Your name is " & FirstName & " " & Surname
Line 4: print Message
Concatenation in VB
Module Module1
Sub Main()
Dim firstName, surName, message As String

Console.Write("Enter your First Name: ")


firstName = Console.ReadLine

Console.Write("Enter your Surname: ")


surName = Console.ReadLine

message = firstName & " " & surName


Console.WriteLine("your name is: " & message)
Console.ReadKey()

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

Data type: INTEGER


CHAR To ASCII in VB.NET
Module Module1

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’

Data type: CHAR


ASCII TO CHAR In VB.NET
Module Module1

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

• returns the character value representing the upper case equivalent of


ThisChar
• Characters that are not lower case alphabetic are returned unchanged
• Example: UCASE('a') returns 'A'
Character into Lower case
• LCASE(ThisChar : CHAR) RETURNS CHAR
• returns the character value representing the lower case
equivalent of ThisChar Characters that are not upper case
alphabetic are returned unchanged
• Example: LCASE('W') returns 'w'
String into Upper Case
• TO_UPPER(ThisString : STRING) RETURNS STRING
• returns a string formed by converting all characters of ThisString to
upper case
• Example: TO_UPPER("Error 803") returns "ERROR 803"
String into Lower case
• TO_LOWER(ThisString : STRING) RETURNS STRING
• returns a string formed by converting all characters of ThisString to
lower case
• Example: TO_LOWER("JIM 803") returns "jim 803"
Changing case of string in VB
Module Module1
Sub Main()
Dim x, a, b As String
a = "faraz alam"
b = "FARAZ ALAM"
x = UCase(a)
Console.WriteLine("upper case of faraz alam is: " & x)
Console.ReadKey()

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

Console.WriteLine("the Uppercase of x is: " & y)


Console.WriteLine("the lowercase of x is: " & z)
Console.ReadKey()

End Sub
End Module

You might also like