提问人:Rajesh 提问时间:9/29/2023 更新时间:9/29/2023 访问量:56
vb.net 代码以根据需要划分字符串
vb.net code to divide the strings as required
问:
我对字符串编程完全陌生,并试图开发一个程序 VB.Net 我无法将学生和讲师与单个领域区分开来。
字段中的示例数据 TextBox1.Text= “1.(汕头大学)科拉蒂·拉贾·戈帕尔 , 2.(汕头大学)米尔扎·沙菲乌拉·拜格 , 3.(LEC)法鲁克·侯赛”
所需输出
TextBox2.Text= “1.(汕头大学)科拉蒂·拉贾·戈帕尔 , 2.(汕头大学)MIRZA SHAFIULLAH BAIG“ //学生 TextBox3.Text=“3.(LEC)FAROOQ HUSSAI“ //讲师
Public FullPName As String
FullPName = UCase(TextBox1.Text)
It1 = FullPName.IndexOf(SExe1)
If It1 <> -1 Then
For It1 = 0 To Len(FullPName - 1)
Get1 = FullPName(It1)
Next
End If
答:
-2赞
Black cat
9/29/2023
#1
此代码片段将两个不同的强制转换选择到两个变量中。根据您的确切要求进行形式化。
Sub STU_LEC()
t1 = "1.(STU) KOLATI RAJA GOPAL , 2.(STU)MIRZA SHAFIULLAH BAIG , 3.(LEC)FAROOQ HUSSAI"
t2 = split(t1, ",")
For i = 0 To UBound(t2)
If InStr(1, t2(i), "(STU)") <> 0 Then
coll2 = coll2 & t2(i)
ElseIf InStr(1, t2(i), "(LEC)") <> 0 Then
coll3 = coll3 & t2(i)
End If
Next i
TextBox2.Text = coll2
TextBox3.Text = coll3
End Sub
0赞
Nick Abbot
9/29/2023
#2
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim aSTringArray() As String
' Split the string by commas, ignoring empties
aSTringArray = TextBox1.Text.Split(CType(",", Char()), StringSplitOptions.RemoveEmptyEntries)
For Each S In aSTringArray
If S.Contains("STU") Then
AppentTextbox(TextBox2, S)
ElseIf S.Contains("LEC") Then
AppentTextbox(TextBox3, S)
End If
Next
End Sub
Private Sub AppentTextbox(TB As TextBox, S As String)
If TB.Text = "" Then
TB.Text &= S
Else
TB.Text &= ", " & S
End If
End Sub
End 类
评论
0赞
Rajesh
9/29/2023
非常感谢 NickAbbot 先生。成功了。
0赞
jmcilhinney
9/29/2023
当您可以使用文本时,为什么要将文本转换为文本?String
Char
Char
0赞
Nick Abbot
9/29/2023
@jmcilhinney因为我的 VS2022 提出了这个建议,我接受了。
0赞
Andrew Morton
9/30/2023
@jmcilhinney .NET Framework 没有重载(.NET Core 2.0+ 有),但它确实有,所以我想这就是 Visual Studio 的建议来源。可惜它没有建议更简洁和可读,但我想有一个我不知道的原因。String.Split(char, StringSplitOptions)
String.Split(char(), StringSplitOptions)
{","c}
0赞
Andrew Morton
9/30/2023
@NickAbbot 如果有类似“4.(LEC)斯图尔特·阿巴迪“,它将进入学生名单。
评论