Topik 4.3 Strings Method
Topik 4.3 Strings Method
NET PROGRAMMING
Topic 4.0
Topic 4.1 : Use Classes, Object and Methods Topic 4.2 : Apply Inheritance And Polymorphism Topic 4.3 : Construct Program Using String Methods Topic 4.4 : Understand the concepts of Exception Handling
Introduction
The String Class represents character strings. The String data type comes from the System.String class . (System.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.
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 .
StringBuilder is a class which is used to handle strings. It Provides Some Standard Function like Append, Reverse, Remove and used for Mutable Strings. The StringBuilder class is defined in the System.Text namespace.
Example
Using StringBuilder class to append and replace function.
Imports System.Text Module Module1 Sub Main() Dim builder As New StringBuilder builder.Append("Rohatash") builder.Append("Kumar") Dim s As String = builder.ToString Console.WriteLine("This defines the append function:=" & s) builder.Replace("ha", "na") Dim m As String = builder.ToString Console.WriteLine("This defines the replace function:=" & m) Console.WriteLine("Length of string is:" + builder.Length.ToString()) End Sub End Module
Output
Strings Function
The Len Function
The length function is used to find out the number of characters in any given string.
Syntax
Len(string) Example Dim leng As String = Len(" Rohatash kumar") Console.WriteLine("length is :" & leng)
Syntax
Mid (string, start[, length])
string - String expression from which characters are returned. start - Long. Character position in string at which the part to be taken begins. length - Length is Optional. Number of characters to return.
Example
Dim middle As String = Mid("Rohatash Kumar", 3, 4) Console.WriteLine("Mid is :" & middle)
Output;??
Syntax
Left("string", n)
Example
Dim leftf As String = Left("Rohatash Kumar", 3) Console.WriteLine("Left is:" & leftf)
Syntax
Right("string", n)
Example
Dim rightr As String = Right("rohatash kumar", 6) Console.WriteLine("Right is :" & rightr)
Syntax
Space (number)
Example
Dim spaces As String = Right("rohatash kumar", 7) Console.WriteLine("Space is :" & spaces)
Syntax
Replace( string, searchtext, replacetext ) Example
Dim replaces As String = Replace("rohatash kumar", "hat", "nmo Console.WriteLine("Replace is :" & replaces)
Syntax
Trim ("String")
Example
Dim trimt As String = Trim(" rohatash kumar ") Console.WriteLine("Trim is :" & trimt)
Syntax
Ltrim("string")
Example
Dim ltriml As String = LTrim(" rohatash kumar Console.WriteLine("ltrim is :" & ltriml) ")
Syntax
Rtrim("string")
Example
Dim rtrimr As String = RTrim(" rohatash kumar Console.WriteLine("rtrim is :" & rtrimr) ")
Example
Dim ucaseu As String = UCase("rohatash kumar") Console.WriteLine("Ucase is :" & ucaseu) Dim lcasel As String = LCase("ROHATASH KUMAR") Console.WriteLine("Ucase is :" & lcasel)
Output:??
Module Module1 Sub Main() Dim leng As String = Len(" Rohatash kumar") Console.WriteLine("length is :" & leng) Dim middle As String = Mid("Rohatash Kumar", 3, 4) Console.WriteLine("Mid is :" & middle) Dim leftf As String = Left("Rohatash Kumar", 3) Console.WriteLine("Left is:" & leftf) Dim rightr As String = Right("rohatash kumar", 6) Console.WriteLine("Right is :" & rightr) Dim spaces As String = Right("rohatash kumar", 7) Console.WriteLine("Space is :" & spaces) Dim replaces As String = Replace("rohatash kumar", "hat", "nmo") Console.WriteLine("Replace is :" & replaces) Dim trimt As String = Trim(" rohatash kumar ") Console.WriteLine("Trim is :" & trimt) Dim ltriml As String = LTrim(" rohatash kumar ") Console.WriteLine("ltrim is :" & ltriml) Dim rtrimr As String = RTrim(" rohatash kumar ") Console.WriteLine("rtrim is :" & rtrimr) Dim ucaseu As String = UCase("rohatash kumar") Console.WriteLine("Ucase is :" & ucaseu) Dim lcasel As String = LCase("ROHATASH KUMAR") Console.WriteLine("Ucase is :" & lcasel) End Sub End Module
Output