本文整理匯總了VB.NET中System.String.Join方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET String.Join方法的具體用法?VB.NET String.Join怎麽用?VB.NET String.Join使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.String
的用法示例。
在下文中一共展示了String.Join方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim maxPrime As Integer = 100
Dim primes As List(Of String) = GetPrimes(maxPrime)
Console.WriteLine("Primes less than {0}:", maxPrime)
Console.WriteLine(" {0}", String.Join(" ", primes))
End Sub
Private Function GetPrimes(maxPrime As Integer) As List(Of String)
Dim values As Array = Array.CreateInstance(GetType(Integer), _
New Integer() { maxPrime - 1}, New Integer(){ 2 })
' Use Sieve of Eratosthenes to determine prime numbers.
For ctr As Integer = values.GetLowerBound(0) To _
CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
If CInt(values.GetValue(ctr)) = 1 Then Continue For
For multiplier As Integer = ctr To maxPrime \ 2
If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
Next
Next
Dim primes As New List(Of String)
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr.ToString())
Next
Return primes
End Function
End Module
輸出:
Primes less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
示例2: modMain
' 導入命名空間
Imports System.Collections.Generic
Imports System.Linq
Module modMain
Public Sub Main()
Dim output As String = String.Join(" ", GetAlphabet(True).Where(Function(letter) _
letter >= "M"))
Console.WriteLine(output)
End Sub
Private Function GetAlphabet(upper As Boolean) As List(Of String)
Dim alphabet As New List(Of String)
Dim charValue As Integer = CInt(IIf(upper, 65, 97))
For ctr As Integer = 0 To 25
alphabet.Add(ChrW(charValue + ctr).ToString())
Next
Return alphabet
End Function
End Module
輸出:
M N O P Q R S T U V W X Y Z
示例3: values
Dim values() As Object = { Nothing, "Cobb", 4189, 11434, .366 }
If values(0) Is Nothing Then values(0) = String.Empty
Console.WriteLine(String.Join("|", values))
輸出:
|Cobb|4189|11434|0.366
示例4: Example
Module Example
Public Sub Main()
Dim maxPrime As Integer = 100
Dim primes() As Integer = GetPrimes(maxPrime)
Console.WriteLine("Primes less than {0}:", maxPrime)
Console.WriteLine(" {0}", String.Join(" ", primes))
End Sub
Private Function GetPrimes(maxPrime As Integer) As Integer()
Dim values As Array = Array.CreateInstance(GetType(Integer), _
New Integer() { maxPrime - 1}, New Integer(){ 2 })
' Use Sieve of Eratosthenes to determine prime numbers.
For ctr As Integer = values.GetLowerBound(0) To _
CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
If CInt(values.GetValue(ctr)) = 1 Then Continue For
For multiplier As Integer = ctr To maxPrime \ 2
If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
Next
Next
Dim primes As New System.Collections.Generic.List(Of Integer)
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
Next
Return primes.ToArray()
End Function
End Module
輸出:
Primes less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
示例5: JoinTest
Public Class JoinTest
Public Shared Sub Main()
Console.WriteLine(MakeLine(0, 5, ", "))
Console.WriteLine(MakeLine(1, 6, " "))
Console.WriteLine(MakeLine(9, 9, ": "))
Console.WriteLine(MakeLine(4, 7, "< "))
End Sub
Private Shared Function MakeLine(initVal As Integer, multVal As Integer, sep As String) As String
Dim sArr(10) As String
Dim i As Integer
For i = initVal To (initVal + 10) - 1
sArr((i - initVal)) = [String].Format("{0,-3}", i * multVal)
Next i
Return [String].Join(sep, sArr)
End Function 'MakeLine
End Class
輸出:
0 , 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 6 12 18 24 30 36 42 48 54 60 81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162 28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91
示例6: Sample
Class Sample
Public Shared Sub Main()
Dim val As [String]() = {"apple", "orange", "grape", "pear"}
Dim sep As [String] = ", "
Dim result As [String]
Console.WriteLine("sep = '{0}'", sep)
Console.WriteLine("val() = {{'{0}' '{1}' '{2}' '{3}'}}", val(0), val(1), val(2), val(3))
result = [String].Join(sep, val, 1, 2)
Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result)
End Sub
End Class
輸出:
sep = ', ' val() = {'apple' 'orange' 'grape' 'pear'} String.Join(sep, val, 1, 2) = 'orange, grape'
示例7: Example
' 導入命名空間
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim maxPrime As Integer = 100
Dim primes As List(Of Integer) = GetPrimes(maxPrime)
Console.WriteLine("Primes less than {0}:", maxPrime)
Console.WriteLine(" {0}", String.Join(" ", primes))
End Sub
Private Function GetPrimes(maxPrime As Integer) As List(Of Integer)
Dim values As Array = Array.CreateInstance(GetType(Integer), _
New Integer() { maxPrime - 1}, New Integer(){ 2 })
' Use Sieve of Eratosthenes to determine prime numbers.
For ctr As Integer = values.GetLowerBound(0) To _
CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))))
If CInt(values.GetValue(ctr)) = 1 Then Continue For
For multiplier As Integer = ctr To maxPrime \ 2
If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier)
Next
Next
Dim primes As New System.Collections.Generic.List(Of Integer)
For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
Next
Return primes
End Function
End Module
輸出:
Primes less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
示例8: Animal
' 導入命名空間
Imports System.Collections.Generic
Public Class Animal
Public Kind As String
Public Order As String
Public Sub New(kind As String, order As String)
Me.Kind = kind
Me.Order = order
End Sub
Public Overrides Function ToString() As String
Return Me.Kind
End Function
End Class
Module Example
Public Sub Main()
Dim animals As New List(Of Animal)
animals.Add(New Animal("Squirrel", "Rodent"))
animals.Add(New Animal("Gray Wolf", "Carnivora"))
animals.Add(New Animal("Capybara", "Rodent"))
Dim output As String = String.Join(" ", animals.Where(Function(animal) _
animal.Order = "Rodent"))
Console.WriteLine(output)
End Sub
End Module
輸出:
Squirrel Capybara