0

I have an array of strings:

array()

and

string = "('one','two','three', ...)"

The values one two three are examples. The string may have more than three words.

How can I fill the array() with:

"one"
"two"
"three"
...
5
  • Drop the first three and last three characters, then split at "','". Commented Feb 25, 2022 at 8:43
  • This would give me then: one','two','three Commented Feb 25, 2022 at 8:44
  • That is why I wrote the "then split at" part Commented Feb 25, 2022 at 8:44
  • woops, just noticed the ' ' haha. How can I drop the first and last letters? Commented Feb 25, 2022 at 8:46
  • Can there be commas inside the strings? Commented Feb 25, 2022 at 11:15

4 Answers 4

2
Dim s = "('one','two','three', ...)"
Dim tokens = From token In s.Trim("(", ")").Split(","c)
             Select token.Trim("'"c)
Dim array() As String = tokens.ToArray()

Remember to add Imports System.Linq

Demo: https://dotnetfiddle.net/Ayy0y1

Note that it's not necessary to use LINQ, i just found it more readable than:

Dim array As String() = s.Trim("(", ")").Split(","c)
For i As Int32 = 0 To array.Length - 1
    array(i) = array(i).Trim("'"c)
Next
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much! Though from line Select and onwards it doesnt seem to work
@Alexander: You have added Imports System.Linq? Added a demo to my answer
@Alexander: also provided a LINQ free version
I forgot to mention I'm using .NET 3.5. I just tried the non Linq version, and the strings still have a space and a ' in front of the strings.
@Alexander: then you use a different char. Just use the correct one in the last Trim. As you see also the non-linq version works: dotnetfiddle.net/eEkZJ3
0

Use String.Split to break up the starting string into an array populated with the split items.

Dim items = "one two three four five"
Dim array = items.Split(" ")
' Outputs array = [ "one", "two", "three", "four", "five" ]

1 Comment

That isn't the pattern of string that the question is asking about.
0

Another solution is with the use of Regex:

Dim str As String = "('one','two','three')"
str = RegularExpressions.Regex.Replace(str, "\('|'\)", "")
Dim array() As String = RegularExpressions.Regex.Split(str, "','")

If there are spaces to remove then:

Dim str As String = "( 'one','two' , 'three ')"
str = RegularExpressions.Regex.Replace(str, "\s+|\(\s*'|'\s*\)", "")
Dim array() As String = RegularExpressions.Regex.Split(str, "','")

Comments

0

Start with the string as you presented it:

Dim myString As String = "('one','two','three')"
Debug.WriteLine(myString)

Output:

('one','two','three')

Next get rid of the unwanted characters by replacing them with empty quotes (nothingness). The Chr(39) function represents the single quote:

myString = myString.Replace("(", "").Replace(")", "").Replace(Chr(39), "")
Debug.WriteLine(myString)

output:

one,two,three

Next convert it to an array with the .Split() method:

Dim myStringArray() As String = myString.Split(",")
For i As Integer = 0 To myStringArray.Length - 1
    Debug.WriteLine(myStringArray(i))
Next i

Output:

one
two
three

This works without any "Imports" statements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.