I have a list like this
Dim emailList as new List(Of String)
emailList.Add("one#domain.com")
emailList.Add("two#domain.com")
emaillist.Add("three#domain.com")
How can I iterate the list with ForEach to get one string with the emails like this
one#domain.com;two#domain.com;three#domain.com
I'm not sure why you would want to use a foreach instead of a String.Join statement. You could simply String.Join() the list using a semi-colon as your joining character.
String.Join(";", emailList.ToArray())
You can try
Dim stringValue As String = String.Join(";", emailList.ToArray)
Have a look at String.Join Method
I wouldn't actually use a ForEach loop for this. Here is what I would do:
String.Join(";", emailList.ToArray());
Dim emailList As New List(Of String)
emailList.Add("one#domain.com")
emailList.Add("two#domain.com")
emailList.Add("three#domain.com")
Dim output As StringBuilder = New StringBuilder
For Each Email As String In emailList
output.Append(IIf(String.IsNullOrEmpty(output.ToString), "", ";") & Email)
Next
Dim emailList As New StringBuilder()
For Each (email As String In emails)
emailList.Append(String.Format("{0};", email))
Next
Return emailList.ToString()
Forgive me if there are any syntax errors ... my VB.NET is a little rusty and I don't have a complier handy.
Related
I'm trying to split a string into a list of strings with this vb.net code, while adding it to a dictionary of String:
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split({",","-"}).ToList)
But I get an error saying "Value of '1-dimentitional arrary of string' cannot be converted to 'Char' "
If I try to implement splitting with just the comma, it works fine. So the following code works.
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split(",").ToList)
But I really want to split the string with two conditions. Any help is appreciated. Thanks!
One of the overloads of the Split Method that takes a String array also needs to have a StringSplitOption parameter also set.
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split(New String() {",", "-"}, StringSplitOptions.None).ToList)
Sub Main()
Dim myString As String = "H,c,J-Hello-World"
Dim myList As List(Of String) = New List(Of String)
myList = myString.Split(New String() {"-", ","}, StringSplitOptions.None).ToList
For Each s As String In myList
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
You need to use the overloaded Split method with an array of char as the separators, like so:
objevt.metalbelowwidth.Item(b).Split(New [Char]() {","c, "-"c })
Demo here
when I do
String.Join(";", lst.Items)
I get a string of object descriptors instead of item of values.
But when I iterate the collection, I end up with a delimiter at the front or back and need to a Substring call afterwards.
Dim res As String = "" 'or use stringbuilder
For Each s As String In lst.Items
s &= ";" & s
Next
res = res.Substring(1)
This applies to other cases as well where you want to turn a shared property within a collection into a delimited string. Is there a nice way to do this?
Can I do this with LINQ and would it be faster?
You'll have to convert the items to strings then:
String.Join(";", lst.Items.Select(Function(item) item.ToString()));
How about
Dim res As String = String.Join(";", lst.Items.OfType(Of String))
This does work:
Dim col As New Collection
col.Add("One")
col.Add("Two")
col.Add("Three")
Dim res = String.Join(";", col.OfType(Of String))
See also this question
Looking for the simplest way to extract values from a string. For example consider the following:
Dim args As String = "/firstname:Bob /lastname:Jones"
To simplify, I need to be able to popup a box that says "Firstname = Bob" or "Lastname = Jones"
Have you tried using the Split method on a string. It should look something like this:
Dim arr() as string
arr=args.Split("/")
Dim i as integer
For i=0 to arr.GetLength(0)
arr(i)=arr(i).Trim.Replace(":", "=")
Next
I would use the Split function to create an array of words, then read them in order:
char[] sep = new char[2];
sep[0] = '/';
sep[1] = ':';
string values = "/firstname:Bob /lastname:Jones";
string[] sites = values.Split(sep);
foreach (string s in sites) {
Console.WriteLine(s);
}
This post is also useful!
http://www.techrepublic.com/article/easily-parse-string-values-with-net/6030362
Using Regex this pattern can help:
(?<identifier>[a-z]+)(?<value>[a-z]+)
See how it works.
You can iterate on all groups and can extract identifier and value.
im getting an exception on this:
Dim strings_extreme As String()
strings_extreme(0) = ""
it says that i am using it before it is being assigned a value
how do i initialize it?
please note that i need to be able to do this:
strings_extreme = input.Split(","c).Distinct().OrderBy(Function(s) s)
If you truly don't know how many strings there are going to be, then why not just use an IList:
Dim stringList As IList(Of String) = New List(Of String)
stringList.Add("")
You can get the Count of how many strings there are and you can For Each through all the strings in the list.
EDIT: If you're just trying to build an array from a Split you should be able to do this:
Dim strings_extreme() As String = input.Split(...)
Dim strings_extreme As List(Of String) = New List(Of String)
strings_extreme.Add("value1")
strings_extreme.Add("value 2")
strings_extreme.Add("value 3rd")
strings_extreme.Add("value again")
Dim strings() As String = strings_extreme.ToArray()
...
Dim strings_extreme(10) As String
strings_extreme(0) = ""
Further info: http://www.startvbdotnet.com/language/arrays.aspx
Dim strings_extreme as String() = {"yourfirstitem"}
But actually, why not take a look at some more advanced data structure from System.Collections namespace?
Shouldn't bother setting a string to "".
Try using:
Dim strings_extreme(10) As String
strings_extreme(yourIndex) = String.Empty
Right - to start with, I'm entering unfamiliar areas with this - so please be kind!
I have a script that looks a little something like this:
Private Function checkString(ByVal strIn As String) As String
Dim astrWords As String() = New String() {"bak", "log", "dfd"}
Dim strOut As String = ""
Dim strWord As String
For Each strWord In astrWords
If strIn.ToLower.IndexOf(strWord.ToLower, 0) >= 0 Then
strOut = strWord.ToLower
Exit For
End If
Next
Return strOut
End Function
It's function is to check the input string and see if any of those 'astrWords' are in there and then return the value.
So I wrote a bit of code to dynamically create those words that goes something like this:
Dim extensionArray As String = ""
Dim count As Integer = 0
For Each item In lstExtentions.Items
If count = 0 Then
extensionArray = extensionArray & """." & item & """"
Else
extensionArray = extensionArray & ", ""." & item & """"
End If
count = count + 1
Next
My.Settings.extensionArray = extensionArray
My.Settings.Save()
Obviously - it's creating that same array using list items. The output of that code is exactly the same as if I hard coded it - but when I change the first bit of code to:
Dim astrWords As String() = New String() {My.Settings.extensionArray}
instead of:
Dim astrWords As String() = New String() {"bak", "log", "dfd"}
It starts looking for the whole statement instead of looping through each individual one?
I think it has something to do with having brackets on the end of the word string - but I'm lost!
Any help appreciated :)
When you use the string from the settings in the literal array, it's just as if you used a single strings containing the delimited strings:
Dim astrWords As String() = New String() {"""bak"", ""log"", ""dfd"""}
What you probably want to do is to put a comma separated string like "bak,log,dfd" in the settings, then you can split it to get it as an array:
Dim astrWords As String() = My.Settings.extensionArray.Split(","C)
You need to set extensionArray up as a string array instead of simply a string.
Note that
Dim something as String
... defines a single string, but
Dim somethingElse as String()
... defines a whole array of strings.
I think with your code, you need something like:
Dim extensionArray As String() = new String(lstExtensions.Items)
Dim count As Integer = 0
For Each item In lstExtentions.Items
extensionArray(count) = item
count = count + 1
Next
My.Settings.extensionArray = extensionArray
My.Settings.Save()
Then at the start of checkString you need something like
Private Function checkString(ByVal strIn As String) As String
Dim astrWords As String() = My.Settings.extensionArray
...
There also might be an even easier way to turn lstExtentions.Items into an Array if Items has a 'ToArray()' method, but I'm not sure what Type you are using there...
What you've done is created a single string containing all 3 words. You need to create an array of strings.
New String() {"bak", "log", "dfd"}
means create a new array of strings containing the 3 strings values "bak", "log" and "dfd".
New String() {My.Settings.extensionArray}
means create a new array of strings containing just one value which is the contents of extensionArray. (Which you have set to ""bak", "log", "dfd""). Note this is one string, not an array of strings. You can't just create 1 string with commas in it, you need to create an array of strings.
If you want to create your array dynamically, you need to define it like this:
Dim astrWords As String() = New String(3)
This creates an array with 3 empty spaces.
You can then assign a string to each space by doing this:
astrWords(0) = "bak"
astrWords(1) = "log"
astrWords(2) = "dfd"
You could do that bit in a for loop:
Dim count As Integer = 0
For Each item In lstExtentions.Items
astrWords(count) = item
count = count + 1
Next
Alternatively, you could look at using a generic collection. That way you could use the Add() method to add multiple strings to it
I think you want your extensionArray to be of type String() and not String. When you are trying to initialize the new array, the initializer doesn't know to parse out multiple values. It just sees your single string.