string 将字符串拆分为 2 个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10788085/
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-09 01:29:25  来源:igfitidea点击:

Split a String into 2 Variables

vb.netstringtextboxsplit

提问by Ron

What I'm trying to do here is Capture 2 Variables from a Textbox

我在这里要做的是从文本框中捕获 2 个变量

Here is an example of whats going to be in here.

下面是一个例子,说明这里将要发生的事情。

User:Pass

User:Pass

I want to declare everything before the :as user and everything after the :as pass.

我想声明:as 用户之前的所有内容以及:as 传递之后的所有内容。

I've Googled, and found a few things, but I couldn't seem to get it working fully.

我用谷歌搜索,发现了一些东西,但我似乎无法让它完全工作。

回答by Kapil Khandelwal

Dim words As String() = textbox1.text.Split(":")
Dim user as String =  words(0)
Dim pass as String =  words(1)

回答by hermiod

Dim str = "User:Pass"

Dim split = str.Split(":")

Dim user as String
Dim password as String

If (split.Count = 2) then
    user=split(0).ToString()
    password = split(1).ToString()
End If

Split on the :, if there are 2 entries in the resulting array, populate the user variable with the first item, and the password variable with the second.

拆分:,如果结果数组中有 2 个条目,则用第一项填充用户变量,用第二项填充密码变量。

回答by peterandree

Dim user As String
Dim pass As String
Dim iPosEQ As Integer
iPosEQ = textbox1.text.IndexOf(":", System.StringComparison.Ordinal)
kv(0) = textbox1.text.Substring(0, iPosEQ - 1)
kv(1) = textbox1.text.Substring(iPosEQ + 1)

This works even with passwords (or users) with ":"

这甚至适用于带有“:”的密码(或用户)