vb.net 将逗号分隔的字符串转换为多个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18045502/
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
Convert Comma-Separated Strings to multiple String
提问by user2502561
I know this has been answered many times , but I was wondering if someone could teach me the simplest approach to splitting a string (comma separated) i.e string number=1,2,3,4,5 ,7,8,9,10
我知道这已被多次回答,但我想知道是否有人可以教我拆分字符串(逗号分隔)的最简单方法,即字符串编号 = 1,2,3,4,5 ,7,8,9,10
such that
以至于
1
2
3
4
5
6
7
8
9
10
after splitting I want to insert that into Database table as separate row(10 rows).I want to ask how I will do that?whether I have to place them into 10 variables?Just getting a bit confused on the iteration over the string!
拆分后,我想将其作为单独的行(10 行)插入到数据库表中。我想问一下我将如何做到这一点?是否必须将它们放入 10 个变量中?只是对字符串的迭代有点困惑!
回答by user2562841
Dim LineOfText As String = "1,2,3,4,5,6,7,8,9,10"
Dim i As Integer
Dim aryTextFile() As String
aryTextFile = LineOfText.Split( "," )
For i = 0 To UBound(aryTextFile)
MsgBox( aryTextFile(i) )
Next i
回答by j_t_fusion
I know its 5 months old but just in case you still need the answer:
我知道它已经 5 个月大了,但以防万一您仍然需要答案:
dim stringNumber as string = "1,2,3,4,5,6,7,8,9,10"
Dim stringSpiltArray as string()
If Not stringNumber Is Nothing And stringNumber.Length <> 0 Then
stringSpiltArray = stringNumber.Split(',')
End If
For Each str As String In stringSpiltArray
'insert into db
Next
Use split()to split up your string and store the separated values in an array.
使用split()分裂的字符串,并分离出的值存储在一个array。
Then use a for eachloop to each item in the array.
然后for each对数组中的每个项目使用循环。
回答by A6SE
Dim substr As String() = txtString.Text.Split(", ")
For Each strnumber As Integer In substr
txtoutput.Text=strnumber & vbNewLine
Next
回答by Venson
You can use the string.Split(...)method to store all results into an string array
您可以使用该string.Split(...)方法将所有结果存储到string array
All of your Choosen languages contains such methods
你所有的选择语言都包含这样的方法
回答by BlackICE
Using the String.split()will return an array of strings .
使用String.split()将返回一个字符串数组。
回答by puneet
you can pass the comma separated string to the db and then split there only. If using sql then you can use the following function
您可以将逗号分隔的字符串传递给数据库,然后仅在那里拆分。如果使用 sql 那么你可以使用下面的函数
CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
回答by Saksham
The below code splits string on the basis of delimiter. You may set StringSplitOptions to either ignore empty entries or add the empty entries to the string array as well.
下面的代码根据分隔符拆分字符串。您可以将 StringSplitOptions 设置为忽略空条目或也将空条目添加到字符串数组中。
//C#
String sentence = "1,2,3,4,5,6,7,8,9";
String[] delim = {","};
String[] words = sentence.Split(delim,StringSplitOptions.RemoveEmptyEntries );
The below code is implemented in C++. It takes a line as input from user and uses strtok to split string based on the delimiters.
下面的代码是用 C++ 实现的。它将一行作为用户的输入,并使用 strtok 根据分隔符拆分字符串。
//C++
string line;
char* word;
getline(cin,line);
word = strtok(const_cast<char*>(line.c_str())," ");
while(word)
{
//input this word in container of your choice
word = strtok(NULL," ");
}

