I am trying to split a string with multiple characters. The string might sometimes contain a - or a /. What I have achieved is the hyphen but I am not able to search for the slash. Any thoughts on how to split the string based on both characters at once ? Once I split after - I add the value after the - to the result list as a separate index and I would like to accomplish the same for '/'.
So For example the Split string has Jet-blue, the below code will add Jet in the result list with index(0) and blue with index(1). In addition to splitting with '-' I would also like to split with '/'. Any suggestions ?
Code:
Dim result As New List(Of String)()
For Each str_get As String In Split
Dim splitStr = str_get.Split("-")
For Each str_split As String In splitStr
result.Add(str_split) ' Enter into result list
' result.TrimExcess()
Next
result.Remove("")
Next
You can either use this or this overload of the Split method.
The first one takes an array of Char:
"Hello World".Split({"e"c, "o"c}) ' Notice the c!
The second one takes an array of String and StringSplitOptions:
"Hello World".Split({"el", "o"}, StringSplitOptions.None)
Related
Function SplitString(ByVal str As String, ByVal numOfChar As Long) As String()
Dim sArr() As String
Dim nCount As Long
ReDim sArr((Len(str) - 1) \ numOfChar)
Do While Len(str)
sArr(nCount) = Left$(str, numOfChar)
str = Mid$(str, numOfChar + 1)
nCount = nCount + 1
Loop
SplitString = sArr
End Function
Hi i was trying to split an array of strings by specific length of character inside a cell with the above code and i got the results..
But now, like for an example, This is our input data "abcd,efghijk,lmn,opqrs,tuvw", and we want to split this into substrings of 8 characters each, after splitting the data, The result will be as "abcd,efg" "hijk,lmn"(8Charcaters), ",opqrs,t(8Charcaters)", "uvw(remaining characters)"... But how i needed was after every splitting, each spliited word should stop with it's previous comma... and next charcaters that come after comma should be considered by next splitted word even it comes on the same length of characters.. like the result for the above example should be "abcd"(should end with this much, even though of 8Charcaters in the splitted word), "efghijk"(correct 8Charcaters)", "lmn"(8Charcaters, even though of 8Charcaters in the splitted word)", "opqrs"(8charcaters even though of 8Charcaters in the splitted word), "tuvw(remaining characters)". We can have multiple words separated by commas inside one substring if all that comes within 8 characters limit. Please advise.
I want to Split a string and then Trim the results in one single line.
So the string:
"psychology ¦ history ¦ geography"
should return the following members without trailing or leading spaces:
psychology
history
geography
I've tried:
String.Split("¦").Trim
but Trim does not work for arrays. Is there a one-line method, no matter how dirty, that does this job?
I'm sure this has been duped many times but I couldn't find the relevant solution for VB.Net. Please don't just link to a question in another language without explaining how I can convert the solution to VB.Net.
You can Split and Trim your string in one line using the LINQ's Select method.
Both assigning the string to a local variable:
Dim input As String = "psychology ¦ history ¦ geography"
Dim result1 = input.Split("¦"c).Select((Function(s) s.Trim))
Or directly using the string as source:
Dim result2 = "psychology ¦ history ¦ geography".Split("¦"c).Select((Function(s) s.Trim))
Note that the Type of result1 and result2 is now an IEnumerable(Of String), not an array:
LINQ's methods return a IEnumerable<T> if not otherwise instructed.
Also note that this syntax supposes that Option Infer is On. If you keep it Off, you need to declare the type explicitly:
Dim result As IEnumerable(Of String) = (...)
If you need a string Array as output, you need to ask:
Dim result3 As String() = input.Split("¦"c).Select((Function(s) s.Trim)).ToArray()
About the Type Characters (c) appended to the Split method parameter:
From the Visual Basic Language Reference: Char Data Type
Type Characters. Appending the literal type character C to a
single-character string literal forces it to the Char data type. Char
has no identifier type character.
If you don't have embedded spaces in the items, you can just pass the space character as a separator as well as pipe. Combine that with the remove empty entries option and multiple space separators aren't an issue. If you need to handle other whitespace characters, you can add those.
input.Split(New String() {"|", " "}, StringSplitOptions.RemoveEmptyEntries)
I am having difficulties trying to figure out the syntax. I have a line of binary in a variable and i want to know how to to split that string every 7th character so i can put it in an ascii table to find its equivalent.
Based on your scenario, you can also do a substring to satisfy your requirement.
Dim theString As String = "w3rs0fd90as90f9sda0faf"
Dim FirstSevenCharacters as String= theString.Substring(0, 7)
Dim CharactersAfterTheFirstSeven as String= theString.Substring(7, theString.Length - 7)
Dim text as string = "Visual Basic"
output: "Basic"
I need to read a string and print it but not starting at the very first index. The text may vary so I want to know if there is a way to read it.
You can use the IndexOf method to find the first space, then the SubString method to get the part of the string after the space:
Dim idx As Integer = text.IndexOf(" "C);
Dim output As String = text.SubString(idx + 1);
(This even works if there would be no space at all in the string. Then idx will be -1 (the value meaning "not found"), so idx + 1 will be zero and output will be the whole string.)
In my application i have string like this 1,2,3&&4,5,6. Now i want check each and every element in single for each loop. Is it possible or not? If its possible how can i acheive this?.
Am trying using split method. But if i am using split method i want more than loop.
Like
dim sa as string=1,2,3&&4,5,6
for each x as string in sa.split("&&")
for each y as string in x.split(",")
''' Here My Process
next
next
How can over come this?. how can change to single loop?. It is possible or not?.
String.Split has an overload that accepts an array of string delimiters:
Dim input As String = "1,2,3&&4,5,6"
For Each element As String In input.Split({",", "&&"}, StringSplitOptions.None)
'do your processing on (1,2,3,4,5,6)
Next
As I understand, you want to use only one for each instead of using for each in for each.
You can first split by "&&" and then join with ",":
dim sa as string=1,2,3&&4,5,6
dim stringArray = String.Join(",", sa.split("&&")).split(",")
for each x as string in stringArray
end for
You could split using a regular expression as a delimiter:
Imports System.Text.RegularExpressions 'goes at the top of the module
For Each x As String In Regex.Split(sa, "\,|&&")
Where the regular expression means "comma or two ampersands". Note that you need to 'escape' the comma using a backslash; this is because commas do something special in regular expressions.
Don't forget to enclose your string in quotes:
dim sa as string="1,2,3&&4,5,6"
One option would be to split on the "," and the "&" via the Split method and ignore empty entries, as such:
Dim sa As String = "1,2,3&&4,5,6"
Dim split As String() = sa.Split(New Char() {",", "&"}, StringSplitOptions.RemoveEmptyEntries)
For Each value In split
Debug.WriteLine(value)
Next