STRING FUNCTIONS
Introduction
The String class of the .NET framework provides many built-in methods to facilitate the
comparison and manipulation of strings. It is now a trivial matter to get data about a
string, or to create new strings by manipulating current strings. The Visual Basic .NET
language also has inherent methods that duplicate many of these functionalities. String
functions are used in computer programming languages to manipulate a string.
Most computer programming languages that have a string datatype will have some string
functions although there may be other low level ways within each language to handle
strings directly. In object oriented languages, string functions are often implemented as
properties and methods of string objects. In functional and list based languages a string is
represented as a list (of character codes), therefore all list-manipulation procedures could
be considered string functions. However such languages may implement a subset of
explicit string-specific functions as well.
Use of string functions
A string is a sequence of characters. We use " " to mark its beginning and end. The need for
strings arises when we need to display a message to the user after some manipulations.
How to declare a String Variable in visual basic 2005
Dim strName As String
How to assign a string to a string variable
StrName = "John"
How to Display a string
MsgBox (strName)
EXPLAINATION OF STRING CLASS
Other languages may have string functions with similar or exactly the same syntax or parameters
or outcomes. The String Class represents character strings. The String data type comes
from the System.String class . The String type represents a string of Unicode
Characters . The String class is a sealed class , so you cannot inherit another class
from the String class.
STRING FUNCTIONS
The String object is Immutable , it cannot be modified once it created, that means
every time you use any operation in the String object , you create a new String Object.
So if you are in a situation in continuous operation with String Object it is
recommended to use System.Text.StringBuilder class to modify a string without
creating a new object.
NOTE:
All the string functions are “CASE SENSTIVE”
String Functions in JAVA
Java String Class is immutable, i.e. Strings in java, once created and initialized, cannot be
changed on the same reference. A java.lang.String class is final which implies no class and
extend it. The java.lang.String class differs from other classes, one difference being that
the String objects can be used with the += and + operators for concatenation.
Two useful methods for String objects are equals( ) and substring( ). The equals( ) method
is used for testing whether two Strings contain the same value. The substring( ) method is
used to obtain a selected portion of a String.
The class String includes methods for examining individual characters of the sequence, for
comparing strings, for searching strings, for extracting substrings, and for creating a copy
of a string with all characters translated to uppercase or to lowercase. Case mapping relies
heavily on the information provided by the Unicode Consortium's Unicode 3.0
specification. The specification's UnicodeData.txt and SpecialCasing.txt files are used
extensively to provide case mapping.
The Java language provides special support for the string concatenation operator ( + ), and
for conversion of other objects to strings. String concatenation is implemented through the
StringBuffer class and its append method. String conversions are implemented through the
method toString, defined by Object and inherited by all classes in Java.
String class creation in Java
A simple String can be created using a string literal enclosed inside double quotes as
shown; String str1 = “My name is bob”;
STRING FUNCTIONS
String Functions in C#
The .NET Framework provides a set of powerful string functions. These building blocks
can be used to write more complex algorithms for handling string data. However
developers aiming to write fast and efficient string functions must be careful of how they
use those building blocks.
To write efficient string handling functions, it is important to understand the
characteristics of string objects in C#.
String Characteristics in C#
First and foremost it is important to know that strings in .NET are class objects. There is
no difference between the types System.String and string, they are both class objects.
Unlike value types, class objects are stored in the heap (instead of the stack). This is an
important fact because it means that creating a string object can trigger garbage collection,
which is costly in terms of performance. In terms of string functions, this means we want
to avoid creating new strings as much as possible.
However that is easier said than done. Another important thing about strings in .NET is
that they are immutable. This means string objects cannot be modified. To edit a string
object, you have to instead create a new string that will have the modification.
Declaring string arrays
class Program
{
static void Main()
{
// String arrays with 3 elements:
string[] arr1 = new string[] { "one",
"two", "three" }; // A
string[] arr2 = { "one", "two",
"three" }; // B
var arr3 = new string[] { "one", "two",
"three" }; // C
string[] arr4 = new string[3]; // D
arr4[0] = "one";
arr4[1] = "two";
arr4[2] = "three";
STRING FUNCTIONS
}
STRING FUNCTIONS
TYPES OF STRING FUNCTIONS IN Vb.Net
There are number of Functions in String Class, some of common functions are listed given
below :-
i. Concat
ii. Length
iii. Substring
iv. Format
v. Replace
vi. Index of
vii. Contains
viii. Trim
ix. Copy
x. Insert
xi. Remove
xii. To upper / To lower
xiii. Pad left / Pad right
xiv. Split
xv. Reverse
xvi. Val
xvii. Space
xviii. Convert (In ASCI code and from ASCI code)
STRING FUNCTIONS
xix. Get char
xx. InStr
xxi. InStrRev
xxii. Mid
xxiii. Starts with
xxiv. Ends with
xxv. Clone (Makes a cloned copy)
Now the above mentioned functions are discussed with the help of examples.
Concatenation of Strings
This function concatenates two different type of strings
Dim str1 As String
Dim str2 As String
str1 = "My name is "
str2 = "Zeeshan"
MsgBox(String.Concat(str1, str2))
Length Function
STRING FUNCTIONS
The Length method Returns the Length of string
Dim strsize As String
strsize = "Maryam"
MsgBox(strsize.Length)
Substring Function
SUBSTRING function returns the set of characters from any position in the string
Dim str1 As String
str1 = "Welcome"
MsgBox(str1.Substring(0, 3)) ' 0 is starting index and 3 is the length you want to
retrieve
OR
MsgBox(str1.Substring(3)) ' Result is Come (it take last 3 words)
Format Function
The format function formats the string in double
Dim Bal As Double
Dim dblBal As Double
Bal = 0.2
dblBal += 2.235648
MsgBox("The balance is:" & String.Format("{0:n3}", dblBal))
Replace Function
STRING FUNCTIONS
This function replace the substrings of a string by giving the string in inverted commas
you want to replace.
Dim str As String = "Welcome to IBIT Lahore"
Dim strResult As String
strResult = str.Replace(" IBIT Lahore", " Punjab University")
MsgBox(strResult)
Indexof Function
This function Finds the Index of any Word in string
Dim str As String
str = "Pakistan"
'MsgBox(str.IndexOf("k"))
Contains function
This is a sort of condition applied on a string, checks whether the word is present in string
or not.
Dim str As String
str = TextBox1.Text
If str.Contains("r") Then
MsgBox("The Word contains r")
Else
MsgBox("The word doesn't contains r")
End If
STRING FUNCTIONS
Trim Function
This function is used For removing useless characters in string .
Dim str As String
str = "Welcome"
MsgBox(str.Trim("come"))
Copy Function
This function is used to Copy one string from another
Dim str1 As String
'Dim str2 As String
str1 = "Welcome"
'str2 = "IBIT"
MsgBox(String.Copy(str2))
Insert Function
INSERT method is used for adjusting any word in between any point of string.
Dim str1 As String
str1 = "Lhr IBIT "
Dim str2 As String
str2 = str1.Insert(3, " Pakistan")
MsgBox(str2.ToString())
Remove Function
STRING FUNCTIONS
This function is used For removing any unwanted words in string
Dim str As String = "123abc456"
'MsgBox(str.Remove(3, 3))
ToUpper Function
This function is used for Converting string in upper case
'Dim str As String
str = "pakistan"
MsgBox(str.ToUpper())
ToLower Function
This function is used for Converting string in Lower case
Dim str As String
str = "HAVE A NICE DAY"
MsgBox(str.ToLower())
Format Function
This function is used for Formating a string according to your requirement
Dim val As Int16 = 7
Dim name As String = "Mr. John"
Dim number As Double = 45.06F
Dim str As String = String.Format("Days Left : {0}. Current DataTime: {1:u}. \n
String: {2}, Float: {3}", val, DateTime.Now, name, number)
MsgBox(str)
Padding the String
STRING FUNCTIONS
This function is used for Padding the strings to right or left
Dim str1 As String = "24 August"
MsgBox(str1.PadLeft(20, "-"))
Dim str2 As String = "24 August"
MsgBox(str2.PadRight(20, "-"))
Split Function
This function is used to spliting the string in as many nombers as you want
Dim s As String = "Welcome To Punjab University"
‘Split string based on spaces
Dim words As String()
words = s.Split(New Char() {" "})
' Use For Each loop over words and display them
Dim word As String
For Each word In words
MsgBox(word)
Next
Reverse Function
This function will reverse the string
Dim str As String = "1 2 3 4 5"
MsgBox(StrReverse(str))
Val Function
STRING FUNCTIONS
This function will Converts a numeric expression to a number.
Dim str As String
str = (1 + 2 + 3) ^ 2
MsgBox(Val((str))) 'returns 36
Space Function
Fills a string with any number of spaces you want in a string.
Dim str As String
str = "This is a" & Space(5) & "string" 'returns "This string".
MsgBox(str)
Convert a string in ASCI Code
Returns the character code of the first character of a string.
Dim str As String
str = Asc("A") 'returns 65
MsgBox(str)
Convert a ASCI Code in String
Returns the display character of a character code.
Dim stri As String
stri = Chr(77) 'returns "M"
MsgBox(stri)
GetChar Function
STRING FUNCTIONS
This function Returns the character at a specified position in a string, counting from 1
Dim str As String
str = GetChar("This is a String", 11) 'returns "s".
MsgBox(str)
InStr Function
This function Returns the starting position in a string of a substring, counting from 1
Dim str As String
str = InStr("My Name is Zeeshan", "Zeeshan") 'returns 11.
MsgBox(str)
InStrRev Function
Returns the starting position in a string of a substring, searching from the end of the string.
Dim str As String
str = InStrRev("This is a string", "string") 'returns 11.
MsgBox(str)
Mid Function
Mid function Returns a substring from a string, specified as the starting position (counting
from 1) and the number of characters.
Dim str As String
str = Mid("This is purple color", 9, 6) 'returns "Purple"
MsgBox(str)
EndsWith Function
STRING FUNCTIONS
The Ends with function in string tells whether a string ends with the required condition or
not. This function is case sensitive.
Dim str As String
str = "VB.NET TOP 10 BOOKS"
If str.EndsWith("BOOKS") = True Then
MsgBox("The String Ends With 'BOOKS' ")
Else
MsgBox("The String doesnot EndsWith 'BOOKS'")
End If
StartsWith Function
The Starts with function in string tells whether a string starts with the required condition
or not. This function is case sensitive.
Dim str As String
str = "VB.NET TOP 10 BOOKS"
If str.StartsWith("VB.NET") = True Then
MsgBox("The String Starts With 'VB.NET' ")
Else
MsgBox("The String doesnot starts With 'VB.NET'")
End If
Colone Function
This function makes cloned copy of a string. The same copy you can save in another
string by giving its reference
Dim str As String = "This function makes coloned copy"
Dim clonedString As String
clonedString = str.Clone()
MsgBox(clonedString)
STRING FUNCTIONS
CONCLUSION
Through String Functions you can play with strings with different Functions.
You can manipulate the string characters and their formatting according to your
desired choice.