(Visual Basic) 如何将字符串值拆分为字符串数组?

发布于 2025-01-10 02:01:17 字数 221 浏览 0 评论 0原文

我有一个字符串数组:

array()

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

值一二三个是例子。该字符串可能包含三个以上的单词。

如何用以下内容填充 array():

"one"
"two"
"three"
...

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"
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾其所爱 2025-01-17 02:01:17
Dim s = "('one','two','three', ...)"
Dim tokens = From token In s.Trim("(", ")").Split(","c)
             Select token.Trim("'"c)
Dim array() As String = tokens.ToArray()

记得添加 Imports System.Linq

演示:https://dotnetfiddle.net/Ayy0y1

请注意,没有必要使用 LINQ,我只是发现它比以下内容更具可读性:

Dim array As String() = s.Trim("(", ")").Split(","c)
For i As Int32 = 0 To array.Length - 1
    array(i) = array(i).Trim("'"c)
Next
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
心房的律动 2025-01-17 02:01:17

使用 String.Split 将起始字符串分解为一个由拆分项填充的数组。

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

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" ]
拥抱没勇气 2025-01-17 02:01:17

另一个解决方案是使用正则表达式:

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

如果有空格需要删除,则:

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

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, "','")
千紇 2025-01-17 02:01:17

从您提供的字符串开始:

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

输出:

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

接下来通过用空引号(什么都没有)替换不需要的字符来删除它们。 Chr(39) 函数表示单引号:

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

输出:

one,two,three

接下来使用 .Split() 方法将其转换为数组:

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

输出:

one
two
three

无需任何“Imports”语句即可工作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文