VB.NET String – String VB.NET

Get 5 Hours of FREE PREMIUM Videos:

LearnVisualStudio.NET Free Preview


LearnVisualStudio.NET Free Preview: Declaring Variables and Assigning Values

I am a lifetime member of LearnVisualStudio.net and a Premium Plus member of dotNetVideos.net.

LearnVisualStudio.net is awesome because it grows in value each year as more videos are added.

dotNetVideos.net is also great because it focuses a lot on MS Certifications and practical interview questions.


- Wade Harvey (IdealProgrammer.com)

LearnVisualStudio Premium Videos

Visual C# 2010 for Absolute Beginners C# for Absolute Beginners C# for Absolute Beginners Study Guide Visual Basic for Absolute Beginners
Visual Basic Express Edition Study Guide Visual Basic 9.0 Language Enhancements Entity Framework Linq To Sql
Windows Phone 7 Development Unit Testing ASP.NET MVC 3 (In Progress) ASP.NET MVC Hands On Project
ASP.NET For Absolute Beginners Visual Web Developer C# Study Guide Visual Web Developer VB.NET Study Guide ASP.NET Controls Series
ASP.NET AJAX ASP.NET Architecture Series ASP.NET Server Controls Silverlight 4.0
Windows Presentation Foundation Windows Forms Controls Visual Studio 2010 New Features Visual Studio Team System 2008
Visual Studio Team System Features Getting Started With Sql Server Express Edition


dotNetVideos Premium Video Catalog

Interview Questions & Answers Windows AzureASP.NET MVC 4Windows Phone 7 (3 Videos)
Visual Studio 2011 Application Lifecycle ManagementLightSwitchHTML 5SOLID
Visual Studio Add-OnsVisual Studio 2010 ALM Lab ManagementASP.NET 4 Deep Dive Microsoft Enterprise Library 5 with ASP.NET 4 (10 Videos)
Virtualization Techniques for Developer (5 Videos)Test Series - 70-519: Designing and Developing Web Applications Using Microsoft .NET Framework 4 (20+ Videos)Test Series - 70-519: Supplemental
70-513: Test Series (MCTS) Windows Communication Foundation 4 (20+ Videos)70-515:Web Applications Development with Microsoft .NET Framework 4 (20+ Videos)Test Series - 70-516: Accessing Data with Microsoft .NET Framework 4 (20+ Videos)Test Series: 70-432: Microsoft SQL Server 2008, Implementation and Maintenance (20+ Videos)
Test Series: 70-433: SQL Server AdministrationFrom Novice To Professional - C# (25 Videos)From Novice To Professional - VB.NET (25 Videos)




Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

VB.NET String – String VB.NET

Purpose: – Illustrates using and .
Prerequistes:

  1. Install Visual Basic (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind and pubs Database
  4. Attach Northwind Database to Databases in Sql Express
  5. Attach pubs Database to Databases in Sql Express

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb

Instructions:

  1. Use Visual Basic 2008 Express or Standard Edition
  2. Create new project;
    • Click File/New Project
    • Select Console Application Template
    • Select Visual Basic for Language
    • name of project could be VBNET_Syntax.
  3. Add New folder named "Language-Basics"
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: Language-Basics
  4. Add Class Named clsString to Language-Basics folder
    • Right-click Language-Basics folder;
    • add new item;
    • Select class
    • Class name could be clsString
  5. Click on copy code in code below to copy code into clsString.vb
  6. Click on copy code in second set of code below to copy code into Module1.vb
  7. Click green arrow or press F5 to run program

Step 1: Use Copy Code to Cut-n-paste code into clsString.vb

 Visual Basic |  copy code |? 
Public Class clsString
 
    Public Sub Main()
 
        '****************************************************************************************
        ' 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. 
        '****************************************************************************************
 
        Console.WriteLine("Example #1: StartsWith(string) ")
 
        Dim strStartsWithExample As String = "This is a test string"
 
        Console.WriteLine(strStartsWithExample.StartsWith("This")) 'Returns True
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #2: EndsWith(string)
        ' Returns a Boolean value that indicates if the string ends with the specified string. 
        '****************************************************************************************
 
        Console.WriteLine("Example #2: EndsWith(string) ")
 
        Dim strEndsWithExample As String = "This is a test string"
 
        Console.WriteLine(strEndsWithExample.EndsWith("string")) 'Returns True
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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
        '****************************************************************************************
 
        Console.WriteLine("Example #3: IndexOf(string[, startIndex][,count]) ")
 
        Dim strIndexOfExample As String = "This is a test string"
 
        Console.WriteLine(strIndexOfExample.IndexOf("is")) 'Returns 2
 
        Console.WriteLine(strIndexOfExample.IndexOf("xyz")) 'Returns -1
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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
        '****************************************************************************************
 
        Console.WriteLine("Example #4: LastIndexOf(string[, startIndex][,count]) ")
 
        Dim strLastIndexOfExample As String = "This is a test string"
 
        Console.WriteLine(strLastIndexOfExample.LastIndexOf("is")) 'Returns 5
 
        Console.WriteLine(strLastIndexOfExample.LastIndexOf("xyz")) 'Returns -1
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #5: Insert(startIndex, string)
        ' Returns a string with the specified string inserted beginning at the specified position.
        '****************************************************************************************
 
        Console.WriteLine("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"
        Console.WriteLine(strInsertExample.Insert(intIndex, "simple "))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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.
        '****************************************************************************************
 
        Console.WriteLine("Example #6: PadLeft(totalWidth [, paddingCharacter]) ")
 
        Dim strPadLeftExampleA As String = "This is a test string"
        Dim strPadLeftExampleB As String = "short"
        Dim strPadLeftExampleC As String = "longer"
 
 
 
        ' PadLeft "simple" before "test"
        Console.WriteLine(strPadLeftExampleA.PadLeft(35))
        Console.WriteLine(strPadLeftExampleB.PadLeft(35))
        Console.WriteLine(strPadLeftExampleC.PadLeft(35))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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.
        '****************************************************************************************
 
        Console.WriteLine("Example #7: PadRight(totalWidth [, paddingCharacter]) ")
 
        Dim strPadRightExampleA As String = "This is a test string"
        Dim strPadRightExampleB As String = "short"
        Dim strPadRightExampleC As String = "longer"
 
 
 
        ' PadRight "simple" before "test"
        Console.WriteLine(strPadRightExampleA.PadRight(35) + "next column starts here")
        Console.WriteLine(strPadRightExampleB.PadRight(35) + "next column starts here")
        Console.WriteLine(strPadRightExampleC.PadRight(35) + "next column starts here")
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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
        '****************************************************************************************
 
        Console.WriteLine("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"
        Console.WriteLine(strRemoveExample.Remove(intIndex))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #9: Replace(oldString, newString)
        ' Returns a string with all occurences of oldString replaced by newString
        '****************************************************************************************
 
        Console.WriteLine("Example #9: Replace(oldString, newString) ")
 
        Dim strReplaceExample As String = "This is a test string"
 
        ' Replace "This" with "That"
        Console.WriteLine(strReplaceExample.Replace("This", "That"))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' 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.
        '****************************************************************************************
 
        Console.WriteLine("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
        Console.WriteLine(strSubstringExample.Substring(intIndexSubstring))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #11: ToLower
        ' Returns the string in Lowercase
        '****************************************************************************************
 
        Console.WriteLine("Example #11: ToLower")
 
        Dim strToLowerExample As String = "This is a test string"
 
        ' Return Lowercase
        Console.WriteLine(strToLowerExample.ToLower)
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #12: ToUpper
        ' Returns the string in Uppercase
        '****************************************************************************************
 
        Console.WriteLine("Example #12: ToUpper ")
 
        Dim strToUpperExample As String = "This is a test string"
 
        ' Return Uppercase
        Console.WriteLine(strToUpperExample.ToUpper)
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #13: Trim
        ' Returns the string with leading and trailing spaces removed
        '****************************************************************************************
 
        Console.WriteLine("Example #13: Trim ")
 
        Dim strTrimExample As String = "     ABC     "
 
        ' Return Uppercase
        Console.WriteLine(strTrimExample.Trim.Length)
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #14: Length
        ' Returns the string with leading and trailing spaces removed
        '****************************************************************************************
 
        Console.WriteLine("Example #14: Length ")
 
        Dim strLengthExample As String = "     ABC     "
 
        ' Return Uppercase
        Console.WriteLine(strLengthExample.Length)
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        '****************************************************************************************
        ' Example #15: Chars(index)
        ' Gets the character at the specified index
        '****************************************************************************************
 
        Console.WriteLine("Example #15: Chars(index) ")
 
        Dim strCharsExample As String = "ABC"
 
        ' Return Uppercase
        Console.WriteLine(strCharsExample.Chars(1))
 
        'write blank line to make output easier to read
        Console.WriteLine()
 
        'Prevent console from closing before you press enter
        Console.ReadLine()
 
    End Sub
 
 
End Class
 
 

Step 2: Use View Plain to Cut-n-paste code into Module1.vb
 Visual Basic |  copy code |? 
Module Module1
 
    Sub Main()
        '***** Language-Basics *************
 
        Dim myString As New clsString
        myString.Main()
 
 
    End Sub
 
End Module
 

Related posts:

  1. VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
  2. C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
  3. VB.NET ASP.NET StringBuilder Source Code Example VB.NET ASP.NET StringBuilder Source Code Example...
  4. VB.NET String Format DateTime Source Code Example VB.NET String Format DateTime Source Code Example...
  5. C# ASP.NET StringBuilder Source Code Example C# ASP.NET StringBuilder Source Code Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Powered by WP Hashcash

This blog uses the cross-linker plugin developed by Web-Developers.Net