I'm currently splitting on periods in my code, but found a few issues when I ran it. I was wondering how I could split on ". " instead of just "."
Current Code:
Dim words As String() = item.Split(New Char() {"."c})
Dream Code:
Dim words As String() = item.Split(New Char() {". "c})
It doesn't allow me to add that extra space in after the period, is there a workaround?
To expand on that, see how Replace is used first? First you turn the 2 characters into one that is going to be uniquely identifiable, and then you can split it effectively.
Dim words As String() = item.Replace(". ", "|").Split(New Char() {"|"c})
And you can probably simplify it even more like this:
Dim words As String() = item.Replace(". ", "|").Split("|"c)
That assumes you are using Option Strict On - if not, you can simplify it more like this as the string will be converted to a char automatically:
Dim words As String() = item.Replace(". ", "|").Split("|")
You should use the String.Split Method (String(), StringSplitOptions) overload of String.Split:
Dim s = "1. 2. 3. 4.5.6"
Dim a = s.Split({". "}, StringSplitOptions.None)
Console.Write(String.Join(vbCrLf, a))
outputs:
1
2
3
4.5.6
(Depending on the version of Visual Studio, you might need something like New String() {". "} instead of {". "}.)
Related
I'm working with an Attachmate Extra Basic Macro and need to Split() a string. I've tried several variations with Extra! Basic and it seems there is no Split() option. The below code works in Visual Basic; how would I do the same thing in Extra! Basic?
Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next
I am very new to Vb.net, and have found out how to split strings and put the remnants of the split string in an array like so...
Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next
Output:
vb.net
split
test
Now that I've got that figured out, I was wondering if there was a way to split on the SECOND space giving the output...
vb.net split
test
Thanks for your time!
You can do something like this
Dim input = "This is a Long String"
Dim splitted = input.Split(" "c)
Dim result = New List(Of String)()
For x As Integer = 0 To splitted.Length - 1 Step 2
result.Add(String.Join(" ", splitted.Skip(x).Take(2)))
Next
I am using an array list to store my strings. However when I run the program it throws an error : Conversion from type 'String()' to type 'String" is not valid. I am sure I am missing something simple. I thought an ArrayList was a list of objects and that it could be anything that I was sent to the array. e.g. Strings in, Strings out. Here is the code that I am working with.
Dim tempString As New ArrayList()
For Each s As String In tempString
Dim sAry As String() = bufString.Split(New Char() {ChrW(2)})
For i As Int16 = 1 To sAry.Length
xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
Next
Next
I also have come to understand that ArrayList has depreciated, should I be using something else?
Before jumping into your case quickly please give a look into other great answers
What's Wrong with an ArrayList?
.NET: ArrayList vs List
and you can google to know more in this regard...
For known typed collection it is not good to use ArrayLists.
For your case I would suggest generic List collection(List is much more efficient in terms of memory usage)
You can see my below test case for you:
Sub Main()
Dim tempList As New List(Of String) 'Consider List Rather ArrayList as you knw your requirement for Strings collection
tempList.Add("string1string2string3string4") 'for testing purpose I've joined "string1, string2, string3 & string4" with ChrW(2) unicode character
For Each s As String In tempList
Dim sAry As String() = s.Split(New Char() {ChrW(2)})
For i As Int16 = 1 To sAry.Length
'xlCells(CurrentRow, i).Value = sAry(i - 1).ToString()
Console.WriteLine(sAry(i - 1).ToString())
Next
Next
Console.ReadLine()
End Sub
What I want to do is to count the number of words a variable has. It's for a hangman game, and will be separated by commas. So I'm basically hoping that the variable will look something like this:
"hang,man,group,toll,snail"
I'm planning on splitting it with the commas to create an array, but apart from that, I'm totally lost on what to do.
On the other hand, I'm more than happy to see any other suggestions for sorting out words to be used in a hangman game!
You're halfway there.
Dim wordCount as Integer = "hang,man,group,toll,snail".Split(",").Length
This splits it into an array, and then returns the number of elements in that array.
Dim Words as String = "hang,man,group,toll,snail"
Dim Word = Words.Split(",")
So the result will be .. Word(0) = "hang", Word(1) = "man" ... so on ..
use Split like so
Dim words As String() = SOMESTRING.Split(New Char() {","c})
now to find length you can
words.length ' set this to a variable or output
alternativly you can also use the words individually
words(0) ' set this to a variable or output
words(1) ' set this to a variable or output
and so on
You can easily split a string into an array by using the String.Split method.
There are a number of overloads, however the one I use most often is this one:
Dim myString as String = "hang,man,group,toll,snail"
Dim myStringArray as String()
myStringArray = myString.Split(new String { "," }, StringSplitOptions.None)
This will give you a string array of length 5.
Documentation can be found here: http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
Dim Text As String
Dim i As Integer
Dim ary() As String
Text = "hang,man,group,toll,snail"
ary = Text.Split(",")
For i = 0 To UBound(ary)
MsgBox(ary(i)) 'here you will get all the words in the array
Next i
MsgBox(i) 'You will get the number of items in your array
I'm new to Visual Basic .net and I would like some help doing this:
I'm receiving these strings:
!re=.id=*10000AF=name=Down-PBX=parent=Down=packet-mark=pack_pbx=limit-at=256000=queue=PCQ_Down=priority=1=max-limit=512000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true=comment=PBX
!re=.id=*10000B0=name=Up-PBX=parent=Up=packet-mark=pack_pbx=limit-at=256000=queue=PCQ_Up=priority=1=max-limit=512000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=true=disabled=true=comment=PBX
!re=.id=*10000C7=name=Down_Mauro=parent=Down=packet-mark==limit-at=315000=priority=8=max-limit=5000000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true
(Only pasted 3 but there are many more)
And I have them in a String Array called tree().
What I want to do is a function that returns new Array, I guess a multidimensional one, that separates everything between the "=", starting from "name". Like this:
Down-PBX, Down, pack_pbx, 256000, PCQ-DOWN, etc
Up-PBX, UP, pack_pbx, 256000, etc
And so on.
Any help wil be really appreciated!! Thanks.
Assuming that the first value you want is always the 5th member of the array, and you wish to return every 2nd value in the array after that:
Private Function GetParts(source As String) As String()
Dim Parts As String() = source.Split(New Char() {"="C})
Dim Items As New List(Of String)()
For i As Integer = 4 To Parts.Length - 1 Step 2
Items.Add(Parts(i))
Next
Return Items.ToArray()
End Function
Essentially, you start at the 5th part of of the split string, and then iterate every 2nd element after that, adding each one to a generic list of strings. You then return this as an array of string.
I do not have a copy of VB here so it may not work 100%, but it should give you the general gist of what you are trying to achieve. I wrote it in C# and then attempted to convert it to VB.Net (fingers crossed!).
Quite easy:
Dim output = input.Substring(input.IndexOf("name=") + 5) _
.Split("="c) _
.Where(Function(e, i) i Mod 2 = 0) _
.ToArray()
output content:
Down-PBX
Down
pack_pbx
256000
PCQ_Down
1
512000
0
0
00:00:00
false
true
PBX
Multidimensional version:
Dim input() As String = {
"!re=.id=*10000AF=name=Down-PBX=parent=Down=packet-mark=pack_pbx=limit-at=256000=queue=PCQ_Down=priority=1=max-limit=512000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true=comment=PBX",
"!re=.id=*10000B0=name=Up-PBX=parent=Up=packet-mark=pack_pbx=limit-at=256000=queue=PCQ_Up=priority=1=max-limit=512000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=true=disabled=true=comment=PBX",
"!re=.id=*10000C7=name=Down_Mauro=parent=Down=packet-mark==limit-at=315000=priority=8=max-limit=5000000=burst-limit=0=burst-threshold=0=burst-time=00:00:00=invalid=false=disabled=true"
}
Dim output = input.Select(Function(i) i.Substring(i.IndexOf("name=") + 5) _
.Split("="c) _
.Where(Function(e, idx) idx Mod 2 = 0) _
.ToArray()) _
.ToArray()
output is String()() here.