本文整理汇总了VB.NET中System.String类的典型用法代码示例。如果您正苦于以下问题:VB.NET String类的具体用法?VB.NET String怎么用?VB.NET String使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了String类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1:
Dim string1 As String = "This is a string created by assignment."
Console.WriteLine(string1)
Dim string2 As String = "The path is C:\PublicDocuments\Report1.doc"
Console.WriteLine(string2)
输出:
This is a string created by assignment. The path is C:\PublicDocuments\Report1.doc
示例2: chars
Dim chars() As Char = { "w"c, "o"c, "r"c, "d"c }
' Create a string from a character array.
Dim string1 As New String(chars)
Console.WriteLine(string1)
' Create a string that consists of a character repeated 20 times.
Dim string2 As New String("c"c, 20)
Console.WriteLine(string2)
输出:
word cccccccccccccccccccc
示例3:
Dim string1 As String = "Today is " + Date.Now.ToString("D") + "."
Console.WriteLine(string1)
Dim string2 As String = "This is one sentence. " + "This is a second. "
string2 += "This is a third sentence."
Console.WriteLine(string2)
输出:
Today is Tuesday, July 06, 2011. This is one sentence. This is a second. This is a third sentence.
示例4:
Dim sentence As String = "This sentence has five words."
' Extract the second word.
Dim startPosition As Integer = sentence.IndexOf(" ") + 1
Dim word2 As String = sentence.Substring(startPosition,
sentence.IndexOf(" ", startPosition) - startPosition)
Console.WriteLine("Second word: " + word2)
输出:
Second word: sentence
示例5:
Dim dateAndTime As DateTime = #07/06/2011 7:32:00AM#
Dim temperature As Double = 68.3
Dim result As String = String.Format("At {0:t} on {0:D}, the temperature was {1:F1} degrees Fahrenheit.",
dateAndTime, temperature)
Console.WriteLine(result)
输出:
At 7:32 AM on Wednesday, July 06, 2011, the temperature was 68.3 degrees Fahrenheit.
示例6: Example
' 导入命名空间
Imports System.Globalization
Imports System.IO
Module Example
Public Sub Main()
Dim sw As New StreamWriter(".\graphemes.txt")
Dim grapheme As String = ChrW(&H0061) + ChrW(&h0308)
sw.WriteLine(grapheme)
Dim singleChar As String = ChrW(&h00e4)
sw.WriteLine(singleChar)
sw.WriteLine("{0} = {1} (Culture-sensitive): {2}", grapheme, singleChar,
String.Equals(grapheme, singleChar,
StringComparison.CurrentCulture))
sw.WriteLine("{0} = {1} (Ordinal): {2}", grapheme, singleChar,
String.Equals(grapheme, singleChar,
StringComparison.Ordinal))
sw.WriteLine("{0} = {1} (Normalized Ordinal): {2}", grapheme, singleChar,
String.Equals(grapheme.Normalize(),
singleChar.Normalize(),
StringComparison.Ordinal))
sw.Close()
End Sub
End Module
输出:
ä ä ä = ä (Culture-sensitive): True ä = ä (Ordinal): False ä = ä (Normalized Ordinal): True
示例7: Example
Module Example
Public Sub Main()
Dim surrogate As String = ChrW(&hD800) + ChrW(&hDC03)
For ctr As Integer = 0 To surrogate.Length - 1
Console.Write("U+{0:X2} ", Convert.ToUInt16(surrogate(ctr)))
Next
Console.WriteLine()
Console.WriteLine(" Is Surrogate Pair: {0}",
Char.IsSurrogatePair(surrogate(0), surrogate(1)))
End Sub
End Module
输出:
U+D800 U+DC03 Is Surrogate Pair: True
示例8: Example
Module Example
Public Sub Main()
Dim s1 As String = "This string consists of a single short sentence."
Dim nWords As Integer = 0
s1 = s1.Trim()
For ctr As Integer = 0 To s1.Length - 1
If Char.IsPunctuation(s1(ctr)) Or Char.IsWhiteSpace(s1(ctr))
nWords += 1
End If
Next
Console.WriteLine("The sentence{2} {0}{2}has {1} words.",
s1, nWords, vbCrLf)
End Sub
End Module
输出:
The sentence This string consists of a single short sentence. has 8 words.
示例9: Example
Module Example
Public Sub Main()
Dim s1 As String = "This string consists of a single short sentence."
Dim nWords As Integer = 0
s1 = s1.Trim()
For Each ch In s1
If Char.IsPunctuation(ch) Or Char.IsWhiteSpace(ch) Then
nWords += 1
End If
Next
Console.WriteLine("The sentence{2} {0}{2}has {1} words.",
s1, nWords, vbCrLf)
End Sub
End Module
输出:
The sentence This string consists of a single short sentence. has 8 words.
示例10: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Globalization
Module Example
Public Sub Main()
' First sentence of The Mystery of the Yellow Room, by Leroux.
Dim opening As String = "Ce n'est pas sans une certaine émotion que "+
"je commence à raconter ici les aventures " +
"extraordinaires de Joseph Rouletabille."
' Character counters.
Dim nChars As Integer = 0
' Objects to store word count.
Dim chars As New List(Of Integer)()
Dim elements As New List(Of Integer)()
For Each ch In opening
' Skip the ' character.
If ch = ChrW(&h0027) Then Continue For
If Char.IsWhiteSpace(ch) Or Char.IsPunctuation(ch) Then
chars.Add(nChars)
nChars = 0
Else
nChars += 1
End If
Next
Dim te As TextElementEnumerator = StringInfo.GetTextElementEnumerator(opening)
Do While te.MoveNext()
Dim s As String = te.GetTextElement()
' Skip the ' character.
If s = ChrW(&h0027) Then Continue Do
If String.IsNullOrEmpty(s.Trim()) Or (s.Length = 1 AndAlso Char.IsPunctuation(Convert.ToChar(s)))
elements.Add(nChars)
nChars = 0
Else
nChars += 1
End If
Loop
' Display character counts.
Console.WriteLine("{0,6} {1,20} {2,20}",
"Word #", "Char Objects", "Characters")
For ctr As Integer = 0 To chars.Count - 1
Console.WriteLine("{0,6} {1,20} {2,20}",
ctr, chars(ctr), elements(ctr))
Next
End Sub
End Module
输出:
Word # Char Objects Characters 0 2 2 1 4 4 2 3 3 3 4 4 4 3 3 5 8 8 6 8 7 7 3 3 8 2 2 9 8 8 10 2 1 11 8 8 12 3 3 13 3 3 14 9 9 15 15 15 16 2 2 17 6 6 18 12 12
示例11: ToString
Public Overloads Function ToString(fmt As String, provider As IFormatProvider) As String _
Implements IFormattable.ToString
If String.IsNullOrEmpty(fmt) Then fmt = "G"
If provider Is Nothing Then provider = CultureInfo.CurrentCulture
Select Case fmt.ToUpperInvariant()
' Return degrees in Celsius.
Case "G", "C"
Return temp.ToString("F2", provider) + "°C"
' Return degrees in Fahrenheit.
Case "F"
Return (temp * 9 / 5 + 32).ToString("F2", provider) + "°F"
' Return degrees in Kelvin.
Case "K"
Return (temp + 273.15).ToString()
Case Else
Throw New FormatException(
String.Format("The {0} format string is not supported.",
fmt))
End Select
End Function
示例12: Example
' 导入命名空间
Imports System.IO
Imports System.Text
Module Example
Public Sub Main()
Dim rnd As New Random()
Dim str As String = String.Empty
Dim sw As New StreamWriter(".\StringFile.txt",
False, Encoding.Unicode)
For ctr As Integer = 0 To 1000
str += ChrW(rnd.Next(1, &h0530))
If str.Length Mod 60 = 0 Then str += vbCrLf
Next
sw.Write(str)
sw.Close()
End Sub
End Module
示例13: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dat As Date = #3/1/2011#
Dim cultures() As CultureInfo = { CultureInfo.InvariantCulture,
New CultureInfo("en-US"),
New CultureInfo("fr-FR") }
For Each culture In cultures
Console.WriteLine("{0,-12} {1}", If(String.IsNullOrEmpty(culture.Name),
"Invariant", culture.Name),
dat.ToString("d", culture))
Next
End Sub
End Module
输出:
Invariant 03/01/2011 en-US 3/1/2011 fr-FR 01/03/2011
示例14: Example
' 导入命名空间
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(String.Compare("A", "a", StringComparison.CurrentCulture))
Console.WriteLine(String.Compare("A", "a", StringComparison.Ordinal))
End Sub
End Module
输出:
1 -32
示例15: Example
' 导入命名空间
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Module Example
Public Sub Main()
Dim strings() As String = { "coop", "co-op", "cooperative",
"co" + ChrW(&h00AD) + "operative",
"cœur", "coeur" }
' Perform a word sort using the current (en-US) culture.
Dim current(strings.Length - 1) As String
strings.CopyTo(current, 0)
Array.Sort(current, StringComparer.CurrentCulture)
' Perform a word sort using the invariant culture.
Dim invariant(strings.Length - 1) As String
strings.CopyTo(invariant, 0)
Array.Sort(invariant, StringComparer.InvariantCulture)
' Perform an ordinal sort.
Dim ordinal(strings.Length - 1) As String
strings.CopyTo(ordinal, 0)
Array.Sort(ordinal, StringComparer.Ordinal)
' Perform a string sort using the current culture.
Dim stringSort(strings.Length - 1) As String
strings.CopyTo(stringSort, 0)
Array.Sort(stringSort, new SCompare())
' Display array values
Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}",
"Original", "Word Sort", "Invariant Word",
"Ordinal Sort", "String Sort")
Console.WriteLine()
For ctr As Integer = 0 To strings.Length - 1
Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}",
strings(ctr), current(ctr), invariant(ctr),
ordinal(ctr), stringSort(ctr))
Next
End Sub
End Module
' IComparer<String> implementation to perform string sort.
Friend Class SCompare : Implements IComparer(Of String)
Public Function Compare(x As String, y As String) As Integer _
Implements IComparer(Of String).Compare
Return CultureInfo.CurrentCulture.CompareInfo.Compare(x, y, CompareOptions.StringSort)
End Function
End Class
输出:
Original Word Sort Invariant Word Ordinal Sort String Sort coop cœur cœur co-op co-op co-op coeur coeur coeur cœur cooperative coop coop coop coeur cooperative co-op co-op cooperative coop cœur cooperative cooperative cooperative cooperative coeur cooperative cooperative cœur cooperative