我正在使用vb.net并从mysql数据库中读取一个字符串。现在该字符串包含"test 1"
。我希望将最后一位数字(1)增加值1.我以某种方式使用字符串长度并解决它但它仅适用于字符串"test 10"
,然后它不断添加空格,例如:"test 13"
等等。我想让字符串自动递增,如"test 20"
或"test 100"
。我怎么能这样做?
这是我正在使用的代码:
If reader2.Read Then
var = reader2.GetInt64("version")
a = reader2.GetString("project_id")
location = reader2.GetString("location")
governorate = reader2.GetString("governorate")
memo = reader2.GetString("memo")
title = reader2.GetString("project_title")
var = var + 1
Dim st As String
st = a.Substring(0, a.Length - 2)
st = st & " " & var
b = st
b应具有“test 2”的值或递增的最终值
答案 0 :(得分:1)
如果在您的情况下投影是“描述值”,则可以使用如下所示的分割功能轻松完成:
var = var + 1
Dim st As String
st = a.Split(" ")(0)
st = st & " " & var
b = st
答案 1 :(得分:0)
核心问题是您正在使用Substring(0, a.Length - 2)
,其中2
是一个常量值,即使数字位数增加也不会增加。
如果项目名称和号码之间有空格,则可以使用String.Split
代替:
Dim version = reader2.GetInt64("version")
Dim projectID = reader2.GetString("project_id")
Dim token As String() = projectID.Split() ' splits by white-space characters '
Dim projectName As String = token(0).Trim()
Dim result As String = String.Format("{0} {1}", projectName, version + 1)
如果您想使用Substring
代替Split
:
' ... '
Dim projectName = projectID
Dim spaceIndex As Int32 = projectName.IndexOf(" ")
If spaceIndex >= 0 Then projectName = projectName.Substring(0, spaceIndex)
' ... '