VB.NET String – String VB.NET
| .NET | .NET Programming (C# or VB) (Web or Desktop) | |||
| Key Supporting Languages | HTML | CSS | JavaScript | SQL Server |
| Access | AJAX | ASP | Blogging | ColdFusion |
| Expression Blend | mySQL | Perl | PHP | Ruby |
| SEO | SharePoint | Silverlight | Web Design | Web Dev |
VB.NET String – String VB.NET
Purpose: – Illustrates using String Properties and String Methods.
Prerequistes:
- Install Visual Basic (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind and pubs Database
- Attach Northwind Database to Databases in Sql Express
- 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:
- Use Visual Basic 2008 Express or Standard Edition
- Create new project;
- Click File/New Project
- Select Console Application Template
- Select Visual Basic for Language
- name of project could be VBNET_Syntax.
- Add New folder named "Language-Basics"
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: Language-Basics
- Add Class Named clsString to Language-Basics folder
- Right-click Language-Basics folder;
- add new item;
- Select class
- Class name could be clsString
- Click on copy code in code below to copy code into clsString.vb
- Click on copy code in second set of code below to copy code into Module1.vb
- 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 | | ? |
| 001 | |
| 002 | Public Class clsString |
| 003 | |
| 004 | Public Sub Main() |
| 005 | |
| 006 | '**************************************************************************************** |
| 007 | ' Example #1: StartsWith(string) |
| 008 | ' Example #2: EndsWith(string) |
| 009 | ' Example #3: IndexOf(string[, startIndex][,count]) |
| 010 | ' Example #4: LastIndexOf(string[, startIndex][,count]) |
| 011 | ' Example #5: Insert(startIndex, string) |
| 012 | ' Example #6: PadLeft(totalWidth [, paddingCharacter]) |
| 013 | ' Example #7: PadRight(totalWidth [, paddingCharacter]) |
| 014 | ' Example #8: Remove(startIndex, count) |
| 015 | ' Example #9: Replace(oldString, newString) |
| 016 | ' Example #10: Substring(startIndex[, length]) |
| 017 | ' Example #11: ToLower |
| 018 | ' Example #12: ToUpper |
| 019 | ' Example #13: Trim |
| 020 | ' Example #14: Length |
| 021 | ' Example #15: Chars(index) |
| 022 | '**************************************************************************************** |
| 023 | |
| 024 | |
| 025 | '**************************************************************************************** |
| 026 | ' Example #1: StartsWith(string) |
| 027 | ' Returns a Boolean value that indicates if the string starts with the specified string. |
| 028 | '**************************************************************************************** |
| 029 | |
| 030 | Console.WriteLine("Example #1: StartsWith(string) ") |
| 031 | |
| 032 | Dim strStartsWithExample As String = "This is a test string" |
| 033 | |
| 034 | Console.WriteLine(strStartsWithExample.StartsWith("This")) 'Returns True |
| 035 | |
| 036 | 'write blank line to make output easier to read |
| 037 | Console.WriteLine() |
| 038 | |
| 039 | '**************************************************************************************** |
| 040 | ' Example #2: EndsWith(string) |
| 041 | ' Returns a Boolean value that indicates if the string ends with the specified string. |
| 042 | '**************************************************************************************** |
| 043 | |
| 044 | Console.WriteLine("Example #2: EndsWith(string) ") |
| 045 | |
| 046 | Dim strEndsWithExample As String = "This is a test string" |
| 047 | |
| 048 | Console.WriteLine(strEndsWithExample.EndsWith("string")) 'Returns True |
| 049 | |
| 050 | 'write blank line to make output easier to read |
| 051 | Console.WriteLine() |
| 052 | |
| 053 | '**************************************************************************************** |
| 054 | ' Example #3: IndexOf(string[, startIndex][,count]) |
| 055 | ' Returns an integer that represents the position of the first occurrence of the |
| 056 | ' specified number of characters. If you specify startIndex, that specifies |
| 057 | ' where to start looking. If you specify count, that specifies how many characters to |
| 058 | ' to search past start. If the string is not found, this method returns -1 |
| 059 | '**************************************************************************************** |
| 060 | |
| 061 | Console.WriteLine("Example #3: IndexOf(string[, startIndex][,count]) ") |
| 062 | |
| 063 | Dim strIndexOfExample As String = "This is a test string" |
| 064 | |
| 065 | Console.WriteLine(strIndexOfExample.IndexOf("is")) 'Returns 2 |
| 066 | |
| 067 | Console.WriteLine(strIndexOfExample.IndexOf("xyz")) 'Returns -1 |
| 068 | |
| 069 | 'write blank line to make output easier to read |
| 070 | Console.WriteLine() |
| 071 | |
| 072 | '**************************************************************************************** |
| 073 | ' Example #4: LastIndexOf(string[, startIndex][,count]) |
| 074 | ' Returns an integer that represents the position of the last occurrence of the |
| 075 | ' specified number of characters. If you specify startIndex, that specifies |
| 076 | ' where to start looking. If you specify count, that specifies how many characters to |
| 077 | ' to search past start. If the string is not found, this method returns -1 |
| 078 | '**************************************************************************************** |
| 079 | |
| 080 | Console.WriteLine("Example #4: LastIndexOf(string[, startIndex][,count]) ") |
| 081 | |
| 082 | Dim strLastIndexOfExample As String = "This is a test string" |
| 083 | |
| 084 | Console.WriteLine(strLastIndexOfExample.LastIndexOf("is")) 'Returns 5 |
| 085 | |
| 086 | Console.WriteLine(strLastIndexOfExample.LastIndexOf("xyz")) 'Returns -1 |
| 087 | |
| 088 | 'write blank line to make output easier to read |
| 089 | Console.WriteLine() |
| 090 | |
| 091 | '**************************************************************************************** |
| 092 | ' Example #5: Insert(startIndex, string) |
| 093 | ' Returns a string with the specified string inserted beginning at the specified position. |
| 094 | '**************************************************************************************** |
| 095 | |
| 096 | Console.WriteLine("Example #5: Insert(startIndex, string) ") |
| 097 | |
| 098 | Dim strInsertExample As String = "This is a test string" |
| 099 | Dim intIndex As Integer = 0 |
| 100 | |
| 101 | ' First find position of word test |
| 102 | intIndex = strInsertExample.IndexOf("test") |
| 103 | |
| 104 | ' Insert "simple" before "test" |
| 105 | Console.WriteLine(strInsertExample.Insert(intIndex, "simple ")) |
| 106 | |
| 107 | 'write blank line to make output easier to read |
| 108 | Console.WriteLine() |
| 109 | |
| 110 | '**************************************************************************************** |
| 111 | ' Example #6: PadLeft(totalWidth [, paddingCharacter]) |
| 112 | ' Returns a string that is right-aligned and padded on left with character specified. |
| 113 | ' If no padddingCharacter is specified, space is used. totalWidth is total width of column |
| 114 | ' This allows you to create a nice straight column even though lengths of text vary. |
| 115 | '**************************************************************************************** |
| 116 | |
| 117 | Console.WriteLine("Example #6: PadLeft(totalWidth [, paddingCharacter]) ") |
| 118 | |
| 119 | Dim strPadLeftExampleA As String = "This is a test string" |
| 120 | Dim strPadLeftExampleB As String = "short" |
| 121 | Dim strPadLeftExampleC As String = "longer" |
| 122 | |
| 123 | |
| 124 | |
| 125 | ' PadLeft "simple" before "test" |
| 126 | Console.WriteLine(strPadLeftExampleA.PadLeft(35)) |
| 127 | Console.WriteLine(strPadLeftExampleB.PadLeft(35)) |
| 128 | Console.WriteLine(strPadLeftExampleC.PadLeft(35)) |
| 129 | |
| 130 | 'write blank line to make output easier to read |
| 131 | Console.WriteLine() |
| 132 | |
| 133 | '**************************************************************************************** |
| 134 | ' Example #7: PadRight(totalWidth [, paddingCharacter]) |
| 135 | ' Returns a string that is left-aligned and padded on right with character specified. |
| 136 | ' If no padddingCharacter is specified, space is used. totalWidth is total width of column |
| 137 | ' This allows you to create a nice straight column even though lengths of text vary. |
| 138 | '**************************************************************************************** |
| 139 | |
| 140 | Console.WriteLine("Example #7: PadRight(totalWidth [, paddingCharacter]) ") |
| 141 | |
| 142 | Dim strPadRightExampleA As String = "This is a test string" |
| 143 | Dim strPadRightExampleB As String = "short" |
| 144 | Dim strPadRightExampleC As String = "longer" |
| 145 | |
| 146 | |
| 147 | |
| 148 | ' PadRight "simple" before "test" |
| 149 | Console.WriteLine(strPadRightExampleA.PadRight(35) + "next column starts here") |
| 150 | Console.WriteLine(strPadRightExampleB.PadRight(35) + "next column starts here") |
| 151 | Console.WriteLine(strPadRightExampleC.PadRight(35) + "next column starts here") |
| 152 | |
| 153 | 'write blank line to make output easier to read |
| 154 | Console.WriteLine() |
| 155 | |
| 156 | '**************************************************************************************** |
| 157 | ' Example #8: Remove(startIndex, count) |
| 158 | ' Returns a string with the specified number of characters removed starting at startIndex. |
| 159 | ' If no Count is specified, the remainder of string is removed |
| 160 | '**************************************************************************************** |
| 161 | |
| 162 | Console.WriteLine("Example #8: Remove(startIndex, count) ") |
| 163 | |
| 164 | Dim strRemoveExample As String = "This is a test string" |
| 165 | Dim intIndexRemove As Integer = 0 |
| 166 | |
| 167 | ' First find position of word test |
| 168 | intIndexRemove = strRemoveExample.IndexOf("test") |
| 169 | |
| 170 | ' Remove rest of string starting with word "test" |
| 171 | Console.WriteLine(strRemoveExample.Remove(intIndex)) |
| 172 | |
| 173 | 'write blank line to make output easier to read |
| 174 | Console.WriteLine() |
| 175 | |
| 176 | '**************************************************************************************** |
| 177 | ' Example #9: Replace(oldString, newString) |
| 178 | ' Returns a string with all occurences of oldString replaced by newString |
| 179 | '**************************************************************************************** |
| 180 | |
| 181 | Console.WriteLine("Example #9: Replace(oldString, newString) ") |
| 182 | |
| 183 | Dim strReplaceExample As String = "This is a test string" |
| 184 | |
| 185 | ' Replace "This" with "That" |
| 186 | Console.WriteLine(strReplaceExample.Replace("This", "That")) |
| 187 | |
| 188 | 'write blank line to make output easier to read |
| 189 | Console.WriteLine() |
| 190 | |
| 191 | '**************************************************************************************** |
| 192 | ' Example #10: Substring(startIndex[, length]) |
| 193 | ' Returns the string that starts at the specified position and has the specified |
| 194 | ' length. If the length is not specified, all of the characters to the end are returned. |
| 195 | '**************************************************************************************** |
| 196 | |
| 197 | Console.WriteLine("Example #10: Substring(startIndex[, length]) ") |
| 198 | |
| 199 | Dim strSubstringExample As String = "This is a test string" |
| 200 | Dim intIndexSubstring As Integer = 0 |
| 201 | |
| 202 | ' First find position of word test |
| 203 | intIndexSubstring = strRemoveExample.IndexOf("test") |
| 204 | |
| 205 | ' Get everything starting at "test" to end of string |
| 206 | Console.WriteLine(strSubstringExample.Substring(intIndexSubstring)) |
| 207 | |
| 208 | 'write blank line to make output easier to read |
| 209 | Console.WriteLine() |
| 210 | |
| 211 | '**************************************************************************************** |
| 212 | ' Example #11: ToLower |
| 213 | ' Returns the string in Lowercase |
| 214 | '**************************************************************************************** |
| 215 | |
| 216 | Console.WriteLine("Example #11: ToLower") |
| 217 | |
| 218 | Dim strToLowerExample As String = "This is a test string" |
| 219 | |
| 220 | ' Return Lowercase |
| 221 | Console.WriteLine(strToLowerExample.ToLower) |
| 222 | |
| 223 | 'write blank line to make output easier to read |
| 224 | Console.WriteLine() |
| 225 | |
| 226 | '**************************************************************************************** |
| 227 | ' Example #12: ToUpper |
| 228 | ' Returns the string in Uppercase |
| 229 | '**************************************************************************************** |
| 230 | |
| 231 | Console.WriteLine("Example #12: ToUpper ") |
| 232 | |
| 233 | Dim strToUpperExample As String = "This is a test string" |
| 234 | |
| 235 | ' Return Uppercase |
| 236 | Console.WriteLine(strToUpperExample.ToUpper) |
| 237 | |
| 238 | 'write blank line to make output easier to read |
| 239 | Console.WriteLine() |
| 240 | |
| 241 | '**************************************************************************************** |
| 242 | ' Example #13: Trim |
| 243 | ' Returns the string with leading and trailing spaces removed |
| 244 | '**************************************************************************************** |
| 245 | |
| 246 | Console.WriteLine("Example #13: Trim ") |
| 247 | |
| 248 | Dim strTrimExample As String = " ABC " |
| 249 | |
| 250 | ' Return Uppercase |
| 251 | Console.WriteLine(strTrimExample.Trim.Length) |
| 252 | |
| 253 | 'write blank line to make output easier to read |
| 254 | Console.WriteLine() |
| 255 | |
| 256 | '**************************************************************************************** |
| 257 | ' Example #14: Length |
| 258 | ' Returns the string with leading and trailing spaces removed |
| 259 | '**************************************************************************************** |
| 260 | |
| 261 | Console.WriteLine("Example #14: Length ") |
| 262 | |
| 263 | Dim strLengthExample As String = " ABC " |
| 264 | |
| 265 | ' Return Uppercase |
| 266 | Console.WriteLine(strLengthExample.Length) |
| 267 | |
| 268 | 'write blank line to make output easier to read |
| 269 | Console.WriteLine() |
| 270 | |
| 271 | '**************************************************************************************** |
| 272 | ' Example #15: Chars(index) |
| 273 | ' Gets the character at the specified index |
| 274 | '**************************************************************************************** |
| 275 | |
| 276 | Console.WriteLine("Example #15: Chars(index) ") |
| 277 | |
| 278 | Dim strCharsExample As String = "ABC" |
| 279 | |
| 280 | ' Return Uppercase |
| 281 | Console.WriteLine(strCharsExample.Chars(1)) |
| 282 | |
| 283 | 'write blank line to make output easier to read |
| 284 | Console.WriteLine() |
| 285 | |
| 286 | 'Prevent console from closing before you press enter |
| 287 | Console.ReadLine() |
| 288 | |
| 289 | End Sub |
| 290 | |
| 291 | |
| 292 | End Class |
| 293 | |
| 294 | |
| 295 |
Step 2: Use View Plain to Cut-n-paste code into Module1.vb
| Visual Basic | | copy code | | ? |
| 01 | |
| 02 | Module Module1 |
| 03 | |
| 04 | Sub Main() |
| 05 | '***** Language-Basics ************* |
| 06 | |
| 07 | Dim myString As New clsString |
| 08 | myString.Main() |
| 09 | |
| 10 | |
| 11 | End Sub |
| 12 | |
| 13 | End Module |
| 14 | |
| 15 |
Related posts:
- C# String Examples – String Examples C# C# String Examples - String Examples C#...
- VB.NET StringBuilder Source Code Example VB.NET StringBuilder Source Code Example...
- C# StringBuilder Source Code Example C# StringBuilder Source Code Example...
- VB.NET ASP.NET String Examples – String Examples ASP.NET VB.NET VB.NET ASP.NET String Examples - String Examples ASP.NET VB.NET...
- C# ASP.NET String Examples – String Examples ASP.NET C# C# ASP.NET String Examples - String Examples ASP.NET C#...
Related posts brought to you by Yet Another Related Posts Plugin.
