如何解析vb.net代码中的文本? [英] How to parse the text in vb.net code ?
问题描述
你好,
IM在vb.net中执行代码.我只有一个文本框,标签为First&姓.结构如下.
第一&姓氏:TEXTBOX
第一个单词用于姓氏",第二个单词用于姓氏".现在,我的代码为
Hello there
IM doing code in vb.net. I have a single textbox with the label First & Last Name. The structure be as follows.
First & Last Name: TEXTBOX
The first word be taken for First Name and the second word for Last Name.Now my code be as
Dim sFirstLastName As String = Me.FirstLastNameTextBox.Text
Dim sName() As String = sFirstLastName.Split(" ")
Dim s As String = sName(0)
Dim s1 As String
If sName.Length >= 2 Then
s1 = sName(1)
Else
s1 = ""
如果用户认为"left one space at the beginning of textbox and type 2 words then in the databse FirstNAme be saved as EMPTY and in the second name only be saved"
有人可以帮我吗?
If the user supposed "left one space at the beginning of textbox and type 2 words then in the databse FirstNAme be saved as EMPTY and in the second name only be saved"
can anybody be help me
推荐答案
说明此处 [ ^ ]解释因此:
如果两个定界符相邻,或者在此实例的开头或结尾处找到一个定界符,则对应的数组元素包含Empty.
您应该检查从Split()
方法返回的元素数,如果有两个以上,则丢弃第一个空元素.或者,您可以先修剪文本以清除开头和结尾的空格.
The notes here[^] explain it thus:
If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.
You should check the number of elements returned from theSplit()
method and discard a first empty one if there are more than two. Alternatively you could trim the text first to purge it of leading and trailing spaces.
为什么要用单个文本框执行此操作,我认为您应该将两个文本框放在一起,以防万一它保存在数据库中的单个字段,当您按名字或姓氏搜索记录时,它可能会影响数据库中的过滤器,因此我建议您改用2.
why r u doing that with single textbox i think u should take two text boxes becoz in ur case it saves in a single field in database and it can affect the filter in database when u search the records by first name or last name so i suggest u take 2 instead.
为什么要使用split?为什么不使用字符串简单函数?
Why use split? why not use string simple functions?
TextBox1.Text.Trim()
Dim SpaceIndex As Integer = TextBox1.Text.IndexOf(" ")
MsgBox(SpaceIndex)
Dim First As String = TextBox1.Text.Substring(0, SpaceIndex)
Dim Last As String = TextBox1.Text.Substring(SpaceIndex + 1)
MessageBox.Show(First)
MessageBox.Show(Last)
这篇关于如何解析vb.net代码中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!