vb.net:你能用一个字符串分割一个字符串吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1639189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:30:21  来源:igfitidea点击:

vb.net: can you split a string by a string

.netvb.netstring

提问by l--''''''---------''''''''''''

for example, can i do this:

例如,我可以这样做吗:

split = temp_string.Split("<beginning of record>")

those of you recommended:

你们中的那些人推荐:

split = Regex.Split(temp_string, "< beginning of record >")

its not working. its just returning the first char "<"

它不工作。它只是返回第一个字符“<”

and those of you that recommended:

以及那些推荐的人:

Dim myDelims As String() = New String(){"< beginning of record >"}
split = temp_string.Split(myDelims, StringSplitOptions.None)

this is not working either. it's also returning just the first char

这也不起作用。它也只返回第一个字符

回答by Matthew Jones

Try this:

尝试这个:

string[] myDelims = new string[] { "<beginning of record>" };
split = temp_string.Split(myDelims,StringSplitOptions.None);

Running this through a code converter results in this:

通过代码转换器运行它会导致:

Dim myDelims As String() = New String() { "<beginning of record>" }
split = temp_string.Split(myDelims, StringSplitOptions.None)

You may also need to escape the chevrons, like this:

您可能还需要避开人字形,如下所示:

"\< beginning of record \>"

回答by bdukes

@Matthew Jones' answer in VB.NET

@Matthew Jones在 VB.NET 中的回答

Dim delim as String() = New String(0) { "<beginning of record>" } 
split = temp_string.Split(delim, StringSplitOptions.None)

回答by Matthew Whited

You can write yourself an extension method to make it easier (Based on the answer by Matthew Jones)

您可以为自己编写一个扩展方法以使其更容易(基于Matthew Jones的回答)

(guess I should show an example as well...)

(我想我也应该展示一个例子......)

Dim results = "hay needle hay needle hay".Split("needle")
' results(0) = "hay "
' results(1) = " hay "
' results(2) = " hay"

... C# ...

... C# ...

public static class Tools
{
    public static string[] Split(this string input, params string[] delimiter)
    {
        return input.Split(delimiter, StringSplitOptions.None);
    }
}

... VB.Net ...

... VB.Net ...

Module Tools
    <Extension()> _
    Public Function Split(ByVal input As String, _
                          ByVal ParamArray delimiter As String()) As String()
        Return input.Split(delimiter, StringSplitOptions.None)
    End Function
End Module

回答by Erik

You could look at Regex.Split()-method.

你可以看看 Regex.Split() 方法。

And this seems to work

这似乎有效

  dim s as string = "you have a <funny> thing <funny> going on"
  dim a() as string = Regex.Split(s,"<funny>")
  for each b as string in a 
     Response.Write( b & "<br>")
  next

回答by Fredou

this seem to work

这似乎有效

    Dim myString As String = "aaajjbbbjjccc"
    Dim mySplit() As Char = "jj".ToCharArray
    Dim myResult() As String = myString.Split(mySplit, StringSplitOptions.RemoveEmptyEntries)

回答by inked

If what you're really splitting is XML read into a string, then don't use VB strings to do that work. Use XSLT. VB/C# have methods for rendering XML with XSLT. It'll be much quicker and more reliable.

如果您真正要拆分的是将 XML 读入字符串,那么不要使用 VB 字符串来完成这项工作。使用 XSLT。VB/C# 具有使用 XSLT 呈现 XML 的方法。它会更快,更可靠。

回答by Brian Schroth

I don't think so, it only takes characters. You could do some ugly hack work around and first replace all instance of the string with some character that does not already exist in the string, and then to Split on that character.

我不这么认为,它只需要字符。你可以做一些丑陋的黑客工作,首先用字符串中不存在的一些字符替换字符串的所有实例,然后在该字符上拆分。

Edited to add: I think Regex.Split is able to do the split on a regex, so if you make a simple regex that is simply the string you want to split on, that should work.

编辑添加:我认为 Regex.Split 能够对正则表达式进行拆分,所以如果你制作一个简单的正则表达式,它只是你想要拆分的字符串,那应该可以工作。