英文:
How do I parse a string with both letters and integers into string and integer variables in VB.net?
问题
Sure, here's the translation:
"我正在从文件中读取字符串。每个字符串都有一个字符串组件,后面跟着两个整数组件,由井号分隔。
字符串示例:
"A1-8"
"AN9-16"
"OUT16-24"
"CARD32-40"
有没有一种简单的方法将它们解析为一个字符串变量和两个整数变量?
例如,对于"AN9-16",我想要得到
StrVar = "AN"
IntVar1 = 9
IntVar2 = 16
提前感谢。
我目前还没有尝试过任何方法。我考虑过查看字符串中的每个字符,然后首先按整数分割,然后按井号分割,但那似乎太复杂了。"
英文:
I am reading strings from a file. Each string will have a string component followed by 2 integer components separated by a hash.
String Examples:
"A1-8"
"AN9-16"
"OUT16-24"
"CARD32-40"
Is there a simple way to parse these into a string variable and 2 integer variables?
for example with "AN9-16" I want to get
StrVar = "AN"
IntVar1 = 9
IntVar2 = 16
Thanks in advance.
I haven't really tried anything so far. I considered looking at each char in the string and then splitting it with the first integer and then with the hash, but that seems over complicated.
答案1
得分: 2
这是一个简单的解决方案,演示如何逐个字符遍历字符串以找到分隔符。你可以使用 IsNumeric 来确定文本部分的结束位置。然后,利用该位置,使用 Substring 分隔两个部分。最后,使用 Split 在“-”上分隔数字部分:
子 解析字符串(value 作为 字符串)
整数 位置 = 0
' 查找所有非数字字符
在(value.Substring(位置, 1) 不是 数字) 循环
位置 += 1
循环
' 提取文本部分
文本部分 作为 字符串 = value.Substring(0, 位置)
' 提取数字部分
数字部分 作为 字符串 = value.Substring(位置 + 1)
' 在 - 上分隔数字部分
数字值 作为 字符串() = Strings.Split(数字部分, "-")
MsgBox("文本部分: " & 文本部分 & vbCrLf & "第一个数字值: " & 数字值(0) & vbCrLf & "第二个数字值: " & 数字值(1))
结束 子
这是一个原始的方法,如果你的字符串不符合你描述的格式,它将失败。尽管如此,它演示了如何将问题分解为更容易解决的部分。
更进一步,你可以定义一个类来返回字符串的值:
公共 类 ItemCode
公共 属性 StringValue 作为 字符串
公共 属性 IntegerValue1 作为 整数
公共 属性 IntegerValue2 作为 整数
''' <summary>
''' 创建 <see cref="ItemCode"/> 类的新实例。
''' </summary>
公共 子 新()
结束 子
''' <summary>
''' 创建 <see cref="ItemCode"/> 类的新实例。
''' </summary>
''' <param name="stringValue"></param>
''' <param name="integerValue1"></param>
''' <param name="integerValue2"></param>
公共 子 新(stringValue 作为 字符串, integerValue1 作为 整数, integerValue2 作为 整数)
Me.StringValue = stringValue
Me.IntegerValue1 = integerValue1
Me.IntegerValue2 = integerValue2
结束 子
结束 类
你可以将这个类用作解析函数的返回值。这是一个使用正则表达式的版本,使用一个带有子表达式的模式,将匹配项收集在 4 个组中(组 1、2 和 3 包含你的值,或在没有匹配的地方为空字符串):
公共 函数 使用RegEx获取ItemCode(value 作为 字符串) 作为 ItemCode
Dim regEx 作为 新 Regex("(\D+)(\d*)-(\d+)")
Dim matchCollection 作为 MatchCollection = regEx.Matches(value)
返回 新 ItemCode(matchCollection(0).Groups(1).Value, CInt(matchCollection(0).Groups(2).Value), CInt(matchCollection(0).Groups(3).Value))
结束 函数
最后,使用这个新类更新我的原始答案:
公共 函数 获取ItemCode(value 作为 字符串) 作为 ItemCode
整数 位置 = 0
' 查找所有非数字字符
在(value.Substring(位置, 1) 不是 数字) 循环
位置 += 1
循环
' 提取文本部分
文本部分 作为 字符串 = value.Substring(0, 位置)
' 提取数字部分
数字部分 作为 字符串 = value.Substring(位置 + 1)
' 在 - 上分隔数字部分
数字值 作为 字符串() = Strings.Split(数字部分, "-")
返回 新 ItemCode(文本部分, CInt(数字值(0)), CInt(数字值(1)))
结束 函数
英文:
Here's a simple solution that shows you how you can "walk through" your string one character at a time to find the delimitations.
You can use IsNumeric to determine where the text part ends. Then with that position, use Substring to separate both parts. Finally, use Split to separate the numeric part on the "-":
Sub ParseString(value As String)
Dim position As Integer = 0
' Find all non-numeric characters
Do While Not IsNumeric(value.Substring(position, 1))
position += 1
Loop
' Extract text part
Dim textPart As String = value.Substring(0, position)
' Extract numeric part
Dim numericPart As String = value.Substring(position + 1)
' Split numeric part on -
Dim numericValues As String() = Strings.Split(numericPart, "-")
MsgBox("Text part: " & textPart & vbCrLf & "First numeric value: " & numericValues(0) & vbCrLf & "Second numeric value: " & numericValues(1))
End Sub
This is a primitive way to do things and will fail if your string is not in the format you have described. Nonetheless, it demonstrates how you can break a problem down into easier parts to solve.
Going further, you can define a class to return the values from your string:
Public Class ItemCode
Public Property StringValue As String
Public Property IntegerValue1 As Integer
Public Property IntegerValue2 As Integer
''' <summary>
''' Creates a new instance of the <see cref="ItemCode"/> class.
''' </summary>
Public Sub New()
End Sub
''' <summary>
''' Creates a new instance of the <see cref="ItemCode"/> class.
''' </summary>
''' <param name="stringValue"></param>
''' <param name="integerValue1"></param>
''' <param name="integerValue2"></param>
Public Sub New(stringValue As String, integerValue1 As Integer, integerValue2 As Integer)
Me.StringValue = stringValue
Me.IntegerValue1 = integerValue1
Me.IntegerValue2 = integerValue2
End Sub
End Class
You can use this class as the return value for your parsing function. Here's a version that uses Regular Expressions, using a pattern with subexpressions that that collects the matches in 4 Groups (Groups 1, 2 and 3 contain your values, or empty strings where there's no match):
Public Function GetItemCodeUsingRegEx(value As String) As ItemCode
Dim regEx As New Regex("(\D+)(\d*)-(\d+)")
Dim matchCollection As MatchCollection = regEx.Matches(value)
Return New ItemCode(matchCollection(0).Groups(1).Value, CInt(matchCollection(0).Groups(2).Value), CInt(matchCollection(0).Groups(3).Value))
End Function
Finally, updating my original answer with this new class:
Public Function GetItemCode(value As String) As ItemCode
Dim position As Integer = 0
' Find all non-numeric characters
Do While Not IsNumeric(value.Substring(position, 1))
position += 1
Loop
' Extract text part
Dim textPart As String = value.Substring(0, position)
' Extract numeric part
Dim numericPart As String = value.Substring(position + 1)
' Split numeric part on -
Dim numericValues As String() = Strings.Split(numericPart, "-")
Return New ItemCode(textPart, CInt(numericValues(0)), CInt(numericValues(1)))
End Function
答案2
得分: 0
I looked into regex and found it too complicated for what I'm trying to do. I code as a hobby and taking the time to learn regex has a low ROI for me. I'm sure one of you experts could come up with an expression or two that would do what I need, but this is what I did based on the answer from Etienne.
我研究了正则表达式,发现它对我要做的事情来说太复杂了。编程对我来说只是一项业余爱好,花时间学习正则表达式的回报率很低。我相信你们中的一位专家可以提供一个或两个表达式来完成我需要的工作,但这是我根据Etienne的答案所做的。
Private Sub ParseString(ByVal FromStr As String, ByRef StrVar As String,
ByRef IntVar1 As Integer, ByRef IntVar2 As Integer)
Dim p = 0
Do While Not IsNumeric(FromStr.Substring(p, 1))
p += 1
Loop
StrVar = FromStr.Substring(0, p)
Dim IntVar() = Split(FromStr.Substring(p), "-")
IntVar1 = IntVar(0)
IntVar2 = IntVar(1)
End Sub
英文:
I looked into regex and found it too complicated for what I'm trying to do. I code as a hobby and taking the time to learn regex has a low ROI for me. I'm sure one of you experts could come up with an expression or two that would do what I need, but this is what I did based on the answer from Etienne.
Private Sub ParseString(ByVal FromStr As String, ByRef StrVar As String,
ByRef IntVar1 As Integer, ByRef IntVar2 As Integer)
Dim p = 0
Do While Not IsNumeric(FromStr.Substring(p, 1))
p += 1
Loop
StrVar = FromStr.Substring(0, p)
Dim IntVar() = Split(FromStr.Substring(p), "-")
IntVar1 = IntVar(0)
IntVar2 = IntVar(1)
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论