本文整理汇总了VB.NET中System.String类的典型用法代码示例。如果您正苦于以下问题:VB.NET String类的具体用法?VB.NET String怎么用?VB.NET String使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了String类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:VB.NET开发者,项目名称:System,代码行数:4,代码来源:String 输出:
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)
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:String 输出:
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)
开发者ID:VB.NET开发者,项目名称:System,代码行数:5,代码来源:String 输出:
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)
开发者ID:VB.NET开发者,项目名称:System,代码行数:6,代码来源:String 输出:
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)
开发者ID:VB.NET开发者,项目名称:System,代码行数:5,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:26,代码来源:String 输出:
ä
ä
ä = ä (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
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:15,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:15,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:50,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:21,代码来源:String
示例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
开发者ID:VB.NET开发者,项目名称:System,代码行数:20,代码来源:String
示例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
开发者ID:VB.NET开发者,项目名称:System,代码行数:17,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:String 输出:
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
开发者ID:VB.NET开发者,项目名称:System,代码行数:52,代码来源:String 输出:
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
示例16: Example
' 导入命名空间
Imports System.IO
Imports System.Text
Module Example
Public Sub Main()
Dim rnd As New Random()
Dim sb As New StringBuilder()
Dim sw As New StreamWriter(".\StringFile.txt",
False, Encoding.Unicode)
For ctr As Integer = 0 To 1000
sb.Append(ChrW(rnd.Next(1, &h0530)))
If sb.Length Mod 60 = 0 Then sb.AppendLine()
Next
sw.Write(sb.ToString())
sw.Close()
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:19,代码来源:String
示例17: Example
' 导入命名空间
Imports System.Globalization
Imports System.Threading
Module Example
Const disallowed = "file"
Public Sub Main()
IsAccessAllowed("FILE:\\\c:\users\user001\documents\FinancialInfo.txt")
End Sub
Private Sub IsAccessAllowed(resource As String)
Dim cultures() As CultureInfo = { CultureInfo.CreateSpecificCulture("en-US"),
CultureInfo.CreateSpecificCulture("tr-TR") }
Dim scheme As String = Nothing
Dim index As Integer = resource.IndexOfAny( {"\"c, "/"c })
If index > 0 Then scheme = resource.Substring(0, index - 1)
' Change the current culture and perform the comparison.
For Each culture In cultures
Thread.CurrentThread.CurrentCulture = culture
Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
Console.WriteLine(resource)
Console.WriteLine("Access allowed: {0}",
Not String.Equals(disallowed, scheme, StringComparison.CurrentCultureIgnoreCase))
Console.WriteLine()
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:29,代码来源:String 输出:
Culture: English (United States)
FILE:\\\c:\users\user001\documents\FinancialInfo.txt
Access allowed: False
Culture: Turkish (Turkey)
FILE:\\\c:\users\user001\documents\FinancialInfo.txt
Access allowed: True
示例18: Example
' 导入命名空间
Imports System.Globalization
Imports System.IO
Module Example
Public Sub Main()
Dim sw As New StreamWriter(".\case.txt")
Dim words As String() = { "file", "sıfır", "Dženana" }
Dim cultures() As CultureInfo = { CultureInfo.InvariantCulture,
New CultureInfo("en-US"),
New CultureInfo("tr-TR") }
For Each word In words
sw.WriteLine("{0}:", word)
For Each culture In cultures
Dim name As String = If(String.IsNullOrEmpty(culture.Name),
"Invariant", culture.Name)
Dim upperWord As String = word.ToUpper(culture)
sw.WriteLine(" {0,10}: {1,7} {2, 38}", name,
upperWord, ShowHexValue(upperWord))
Next
sw.WriteLine()
Next
sw.Close()
End Sub
Private Function ShowHexValue(s As String) As String
Dim retval As String = Nothing
For Each ch In s
Dim bytes() As Byte = BitConverter.GetBytes(ch)
retval += String.Format("{0:X2} {1:X2} ", bytes(1), bytes(0))
Next
Return retval
End Function
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:36,代码来源:String 输出:
file:
Invariant: FILE 00 46 00 49 00 4C 00 45
en-US: FILE 00 46 00 49 00 4C 00 45
tr-TR: FİLE 00 46 01 30 00 4C 00 45
sıfır:
Invariant: SıFıR 00 53 01 31 00 46 01 31 00 52
en-US: SIFIR 00 53 00 49 00 46 00 49 00 52
tr-TR: SIFIR 00 53 00 49 00 46 00 49 00 52
Dženana:
Invariant: DžENANA 01 C5 00 45 00 4E 00 41 00 4E 00 41
en-US: DŽENANA 01 C4 00 45 00 4E 00 41 00 4E 00 41
tr-TR: DŽENANA 01 C4 00 45 00 4E 00 41 00 4E 00 41
示例19: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateString As String = "07/10/2011"
Dim cultures() As CultureInfo = { CultureInfo.InvariantCulture,
CultureInfo.CreateSpecificCulture("en-GB"),
CultureInfo.CreateSpecificCulture("en-US") }
Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}", "Date String", "Culture",
"Month", "Day")
Console.WriteLine()
For Each culture In cultures
Dim dat As Date = DateTime.Parse(dateString, culture)
Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}", dateString,
If(String.IsNullOrEmpty(culture.Name),
"Invariant", culture.Name),
dat.Month, dat.Day)
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:21,代码来源:String 输出:
Date String Culture Month Day
07/10/2011 Invariant 7 10
07/10/2011 en-GB 10 7
07/10/2011 en-US 7 10
示例20: Example
' 导入命名空间
Imports System.Globalization
Imports System.Threading
Public Module Example
Public Sub Main()
Dim str1 As String = "Apple"
Dim str2 As String = "Æble"
Dim str3 As String = "AEble"
' Set the current culture to Danish in Denmark.
Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
Console.WriteLine("Current culture: {0}",
CultureInfo.CurrentCulture.Name)
Console.WriteLine("Comparison of {0} with {1}: {2}",
str1, str2, String.Compare(str1, str2))
Console.WriteLine("Comparison of {0} with {1}: {2}",
str2, str3, String.Compare(str2, str3))
Console.WriteLine()
' Set the current culture to English in the U.S.
Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
Console.WriteLine("Current culture: {0}",
CultureInfo.CurrentCulture.Name)
Console.WriteLine("Comparison of {0} with {1}: {2}",
str1, str2, String.Compare(str1, str2))
Console.WriteLine("Comparison of {0} with {1}: {2}",
str2, str3, String.Compare(str2, str3))
Console.WriteLine()
' Perform an ordinal comparison.
Console.WriteLine("Ordinal comparison")
Console.WriteLine("Comparison of {0} with {1}: {2}",
str1, str2,
String.Compare(str1, str2, StringComparison.Ordinal))
Console.WriteLine("Comparison of {0} with {1}: {2}",
str2, str3,
String.Compare(str2, str3, StringComparison.Ordinal))
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:40,代码来源:String 输出:
Current culture: da-DK
Comparison of Apple with Æble: -1
Comparison of Æble with AEble: 1
Current culture: en-US
Comparison of Apple with Æble: 1
Comparison of Æble with AEble: 0
Ordinal comparison
Comparison of Apple with Æble: -133
Comparison of Æble with AEble: 133
注:本文中的System.String类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论