开发者

VB: Need help splitting a string into 3 variables

I need to split one variable into 3 variables. For example I have a variable called Data and it contains AAA00000001. I need to have them be split between the "AAA", "0000000", and "1". I looked at the Split() function but didn't get a clear example for this situation. The reason I need to do this is becaus开发者_JAVA百科e I want to store these 3 variables into fields for a MySQL database.


Are the three subvariables always of the same length?

If so, you can use Substrings:

Dim substring1 As String = Data.Substring(0, 3)
Dim substring2 As String = Data.Substring(3, 7)
Dim substring3 As String = Data.Substring(10, 1)


Assuming the string is ALWAYS the EXACT same length and need to be split at the SAME place, you can use Substring().

dim s as String = "AAA00000001"

dim s1 as String = s.Substring(0, 3)
dim s2 as String = s.Substring(3, 7)
dim s3 as String = s.Substring(10)

If they're not always the same length, you're probably going to need to use Regular Expressions.


Split will break your string apart based on a character, or group of. It's not appropriate here, that is unless you're always splitting on 0000000, which I doubt you are.

If you know that the first 3 characters will always be your first group, second 7 your next, and last character, your final group, you could do something like this.

This uses the Substring function, e.g.

Dim yourString as String = "AAA00000001"
Dim c1 As String = yourString.Substring(0, 3)
Dim c2 As String = yourString.Substring(3, 7)
Dim c3 As String = yourString.Substring(10, 1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜