| hoofdstuk |
15. 16. 17.  |
| onderwerp |
15.13. 16.1. String Datatype 16.2.  |
| rubrieken | 
|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
Hieronder zie je enkele voorbeelden van het gebruik van members uit de System.String klasse om enkel standaard bewerkingen uit te voeren : Visual Basic 2010 Broncode Option Explicit OnOption Strict OnClass Example1 Public Shared Sub Main() Dim text As String = "visual basic .net" Console.WriteLine( text.Length) Console.WriteLine( text.Chars(7)) Console.WriteLine( String.Concat( "course ", text)) Console.WriteLine( text.PadLeft(30)) Console.WriteLine( text.PadRight(30, "."c)) Console.WriteLine( text.Insert(13, "language ")) Console.WriteLine( text.Substring(13)) Console.WriteLine( text.Substring(7, 5)) Console.WriteLine( text.Remove(7, 6)) text = String.Concat( " ", text, " ") Console.WriteLine( text.Trim()) Console.WriteLine( text.TrimStart()) Console.WriteLine( text.TrimEnd()) Console.WriteLine( text.Trim( New Char() { " "c, "v"c, "t"c, "e"c})) Console.WriteLine( text.Replace( "basic", "c#")) text = "Visual Basic .NET" Console.WriteLine( text.ToUpper()) Console.WriteLine( text.ToLower()) Console.WriteLine( text.IndexOf( "Basic")) Console.WriteLine( text.LastIndexOf( "e"c)) Console.WriteLine( text.StartsWith( "Visual")) Console.WriteLine( text.EndsWith( ".Net")) Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output 17
b
course visual basic .net
visual basic .net
visual basic .net.............
visual basic language .net
.net
basic
visual .net
visual basic .net
visual basic .net
visual basic .net
isual basic .n
visual c# .net
VISUAL BASIC .NET
visual basic .net
7
-1
True
FalseAlle bovenvermelde methods uitgezonderd String.Concat() zijn instance function methods. Deze function methods gaan het String object - waarop die method wordt aangeroepen - niet aanpassen, maar zullen het gewijzigde resultaat ( hier doorgaans String object ) opleveren. 16.1.1. Splitting en Joining StringsHieronder zie je enkele voorbeelden van het gebruik van instance- en classmembers uit de String klasse om splitting en joining van strings uit te voeren : Visual Basic 2010 Broncode Class Example2 Public Shared Sub Main() Dim text As String = "Visual-Basic .NET." Dim characters As Char() = text.ToCharArray() For Each character As Char In characters Console.Write(character & "/") Next Console.WriteLine() characters = text.ToCharArray(7, 5) For Each character As Char In characters Console.Write(character & "/") Next Console.WriteLine() Dim words As String() = text.Split( " "c) For Each woord As String In words Console.Write(woord & "/") Next Console.WriteLine() words = text.Split( New Char() { " "c, "-"c}) For Each woord As String In words Console.Write(woord & "/") Next Console.WriteLine() words = text.Split( New Char() { " "c, "-"c}, 2) For Each woord As String In words Console.Write(woord & "/") Next Console.WriteLine() words = text.Split( New Char() { " "c, "-"c}) Console.WriteLine( String.Join( "<space>", words)) Console.WriteLine( String.Join( "<space>", words, 1, 2)) Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output V/i/s/u/a/l/-/B/a/s/i/c/ /./N/E/T/./
B/a/s/i/c/
Visual-Basic/.NET./
Visual/Basic/.NET./
Visual/Basic .NET./
Visual<space>Basic<space>.NET.
Basic<space>.NET.
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|