How to Join a string array?
In vb6 we could simply do Join(sourcearray, delimiter)
But I cant find the equivalent in vb.net, Is there?
----
Also, I'm writing a FireFox proxy changer, and Im looking for input on how I could improve my code:
VB Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Path As String = String.Empty
Dim js_Prefs() As String
Dim RndProxy As New Random
Dim Proxies() As String = (My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Proxies.txt")).Split(CChar(Environment.NewLine))
For Each FxUserName As String In IO.Directory.GetDirectories("C:\Documents and Settings\" & Environment.UserName & "\Application Data\Mozilla\Firefox\Profiles\")
Path = FxUserName
Next
js_Prefs = My.Computer.FileSystem.ReadAllText(Path & "\prefs.js").Split(CChar(Environment.NewLine))
Dim IP As String = Proxies(RndProxy.Next(0, Proxies.GetUpperBound(0))).Trim
For I As Int32 = 0 To js_Prefs.GetUpperBound(0)
If js_Prefs(I).Contains("network.proxy.http_port") Then
js_Prefs(I) = js_Prefs(I).Substring(1, js_Prefs(I).LastIndexOf(",") + 1)
js_Prefs(I) += IP.Substring(IP.IndexOf(":") + 1) & ");"
Exit For
ElseIf js_Prefs(I).Contains("network.proxy.http") Then
js_Prefs(I) = js_Prefs(I).Substring(1, js_Prefs(I).IndexOf(",") + 1)
js_Prefs(I) += Convert.ToChar(34) & IP.Substring(1, IP.IndexOf(":") - 1) & Convert.ToChar(34) & ");"
End If
Next
End Sub
End Class
Re: How to Join a string array?
You can just loop through the array, concatenating it...
VB Code:
Dim BigString as String
For each str as string in MyStringArray()
BigString &= str & Delimiter
Next
Messagebox.Show(BigString)
Re: How to Join a string array?
It still exists, you can use the
VB Code:
Microsoft.VisualBasic.Strings.Join(stringthing)
namespace to get hold of it, or better still, use the real .Net way
VB Code:
System.String.Join(" ", stringthing)
Re: How to Join a string array?
Wow Grimfort, I hadn't ever seen that :) Didn't even know it existed.... :thumb:
Re: How to Join a string array?
Oooo, as another pointer, lookup the StringBuilder class. It allows you to concat strings, much much quicker when doing a lot of looping. For a quick bit of code you can do this:
VB Code:
Dim intIndex As Integer
Dim MeBuilder As New System.Text.StringBuilder
For intIndex = 0 To 10000
MeBuilder.Append("All my test data" & vbCrLf)
Next
'Use the data
testtest = MeBuilder.ToString
Re: How to Join a string array?
Thanks man it works :thumb:
system.string.join