Progress Monitor for IdealProgrammer.com
Take Swift, Intelligent, Massive, Planned, Loving, Effective (S.I.M.P.L.E.) Action to Transform Self into Ideal
Skip Repetitive Navigational Links
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console
Please login
VB.NET ASP.NET String Examples
Description:
Illustrates using String Properties and String Methods in VB.NET ASP.NET.
Example Webform Code:
< %@ Page Language="VB" AutoEventWireup="false" CodeFile="String.aspx.vb" Inherits="LanguageBasics_String" %> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp :Label ID="Label1a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label1" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label2a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label2" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label3a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label3" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label4a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label4" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label5a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label5" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label6a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label6" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label7a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label7" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label8a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label8" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label9a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label9" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label10a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label10" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label11a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label11" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label12a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label12" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label13a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label13" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label14a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label14" runat="server" Text="Label"></asp><br /><br /> <asp :Label ID="Label15a" runat="server" Text="Label"></asp><br /> <asp :Label ID="Label15" runat="server" Text="Label"></asp><br /><br /> </div> </form> </body> </html>
Example Code Behind:
Partial Class LanguageBasics_String Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load '**************************************************************************************** ' Example #1: StartsWith(string) ' Example #2: EndsWith(string) ' Example #3: IndexOf(string[, startIndex][,count]) ' Example #4: LastIndexOf(string[, startIndex][,count]) ' Example #5: Insert(startIndex, string) ' Example #6: PadLeft(totalWidth [, paddingCharacter]) ' Example #7: PadRight(totalWidth [, paddingCharacter]) ' Example #8: Remove(startIndex, count) ' Example #9: Replace(oldString, newString) ' Example #10: Substring(startIndex[, length]) ' Example #11: ToLower ' Example #12: ToUpper ' Example #13: Trim ' Example #14: Length ' Example #15: Chars(index) '**************************************************************************************** '**************************************************************************************** ' Example #1: StartsWith(string) ' Returns a Boolean value that indicates if the string starts with the specified string. '**************************************************************************************** Label1a.Text = "Example #1: StartsWith(string) " Dim strStartsWithExample As String = "This is a test string" Label1.Text = strStartsWithExample.StartsWith("This") 'Returns True '**************************************************************************************** ' Example #2: EndsWith(string) ' Returns a Boolean value that indicates if the string ends with the specified string. '**************************************************************************************** Label2a.Text = "Example #2: EndsWith(string) " Dim strEndsWithExample As String = "This is a test string" Label2.Text = strEndsWithExample.EndsWith("string") 'Returns True '**************************************************************************************** ' Example #3: IndexOf(string[, startIndex][,count]) ' Returns an integer that represents the position of the first occurrence of the ' specified number of characters. If you specify startIndex, that specifies ' where to start looking. If you specify count, that specifies how many characters to ' to search past start. If the string is not found, this method returns -1 '**************************************************************************************** Label3a.Text = "Example #3: IndexOf(string[, startIndex][,count]) " Dim strIndexOfExample As String = "This is a test string" Label3.Text = strIndexOfExample.IndexOf("is") 'Returns 2 '**************************************************************************************** ' Example #4: LastIndexOf(string[, startIndex][,count]) ' Returns an integer that represents the position of the last occurrence of the ' specified number of characters. If you specify startIndex, that specifies ' where to start looking. If you specify count, that specifies how many characters to ' to search past start. If the string is not found, this method returns -1 '**************************************************************************************** Label4a.Text = "Example #4: LastIndexOf(string[, startIndex][,count]) " Dim strLastIndexOfExample As String = "This is a test string" Label4.Text = strLastIndexOfExample.LastIndexOf("is") 'Returns 5 '**************************************************************************************** ' Example #5: Insert(startIndex, string) ' Returns a string with the specified string inserted beginning at the specified position. '**************************************************************************************** Label5a.Text = "Example #5: Insert(startIndex, string) " Dim strInsertExample As String = "This is a test string" Dim intIndex As Integer = 0 ' First find position of word test intIndex = strInsertExample.IndexOf("test") ' Insert "simple" before "test" Label5.Text = strInsertExample.Insert(intIndex, "simple ") '**************************************************************************************** ' Example #6: PadLeft(totalWidth [, paddingCharacter]) ' Returns a string that is right-aligned and padded on left with character specified. ' If no padddingCharacter is specified, space is used. totalWidth is total width of column ' This allows you to create a nice straight column even though lengths of text vary. '**************************************************************************************** Label6a.Text = "Example #6: PadLeft(totalWidth [, paddingCharacter]) " Dim strPadLeftExampleA As String = "This is a test string" ' PadLeft "simple" before "test" Label6.Text = strPadLeftExampleA.PadLeft(35) '**************************************************************************************** ' Example #7: PadRight(totalWidth [, paddingCharacter]) ' Returns a string that is left-aligned and padded on right with character specified. ' If no padddingCharacter is specified, space is used. totalWidth is total width of column ' This allows you to create a nice straight column even though lengths of text vary. '**************************************************************************************** Label7a.Text = "Example #7: PadRight(totalWidth [, paddingCharacter]) " Dim strPadRightExampleA As String = "This is a test string" ' PadRight "simple" before "test" Label7.Text = strPadRightExampleA.PadRight(35) + "next column starts here" '**************************************************************************************** ' Example #8: Remove(startIndex, count) ' Returns a string with the specified number of characters removed starting at startIndex. ' If no Count is specified, the remainder of string is removed '**************************************************************************************** Label8a.Text = "Example #8: Remove(startIndex, count) " Dim strRemoveExample As String = "This is a test string" Dim intIndexRemove As Integer = 0 ' First find position of word test intIndexRemove = strRemoveExample.IndexOf("test") ' Remove rest of string starting with word "test" Label8.Text = strRemoveExample.Remove(intIndex) '**************************************************************************************** ' Example #9: Replace(oldString, newString) ' Returns a string with all occurences of oldString replaced by newString '**************************************************************************************** Label9a.Text = "Example #9: Replace(oldString, newString) " Dim strReplaceExample As String = "This is a test string" ' Replace "This" with "That" Label9.Text = strReplaceExample.Replace("This", "That") '**************************************************************************************** ' Example #10: Substring(startIndex[, length]) ' Returns the string that starts at the specified position and has the specified ' length. If the length is not specified, all of the characters to the end are returned. '**************************************************************************************** Label10a.Text = "Example #10: Substring(startIndex[, length]) " Dim strSubstringExample As String = "This is a test string" Dim intIndexSubstring As Integer = 0 ' First find position of word test intIndexSubstring = strRemoveExample.IndexOf("test") ' Get everything starting at "test" to end of string Label10.Text = strSubstringExample.Substring(intIndexSubstring) '**************************************************************************************** ' Example #11: ToLower ' Returns the string in Lowercase '**************************************************************************************** Label11a.Text = "Example #11: ToLower" Dim strToLowerExample As String = "This is a test string" ' Return Lowercase Label11.Text = strToLowerExample.ToLower '**************************************************************************************** ' Example #12: ToUpper ' Returns the string in Uppercase '**************************************************************************************** Label12a.Text = "Example #12: ToUpper " Dim strToUpperExample As String = "This is a test string" ' Return Uppercase Label12.Text = strToUpperExample.ToUpper '**************************************************************************************** ' Example #13: Trim ' Returns the string with leading and trailing spaces removed '**************************************************************************************** Label13a.Text = "Example #13: Trim " Dim strTrimExample As String = " ABC " ' Return Uppercase Label13.Text = strTrimExample.Trim.Length '**************************************************************************************** ' Example #14: Length ' Returns the string with leading and trailing spaces removed '**************************************************************************************** Label14a.Text = "Example #14: Length " Dim strLengthExample As String = " ABC " ' Return Uppercase Label14.Text = strLengthExample.Length '**************************************************************************************** ' Example #15: Chars(index) ' Gets the character at the specified index '**************************************************************************************** Label15a.Text = "Example #15: Chars(index) " Dim strCharsExample As String = "ABC" ' Return Uppercase Label15.Text = strCharsExample.Chars(1) End Sub End Class
Example Connection String:
<connectionstrings> <add name="Northwind_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI" /> <add name="Pubs_ConnectionString" connectionString="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI" /> </connectionstrings>
Home
News
Presenters
Register
Contact
Categories
Titles
Converter
Code Samples
C# ASP.NET
C# Console
HTML
JavaScript
SQL Server
VB ASP.NET
VB Console