如何在字符串中找到字符串,Instr是VB6方式... [英] How Can I find a String within a String, Instr was the VB6 way...
问题描述
大家好,
我想确保我只是读取一个响应的数字部分,它会以字符串1.95e-3mA的形式返回为了得到这个,我一直在使用子串来修剪最后一部分,这个已经回复的板子刚刚回复了1.9e-3mA意味着我的小心
result = dataBack。子串( 0 , 3 )
有我现在可以看到这一切都崩溃了,因为回复现在是1.9e(打破了计算)而不是1.95。有没有一种简单的方法可以做到这一点,或者我必须重新思考我这样做的方式。 Val()会解决我的问题吗?...
格伦
Val()不会因为我需要成为一个字符串我想知道Val它能得到数字和转换它返回一个字符串??? ....
如果数字部分后面总是跟着e
,那么你可以使用 String.Split [ ^ ]将字符串拆分成一个数组,然后取出数组的第一部分:
Dim 输入作为 字符串 = 1.9e-3mA
Dim 结果 As String = input.Split(< span class =code-string> e c)( 0 )
但你在评论中说它不会总是e
,所以你可以使用 Regex.Split [ ^ ]按正则表达式分割:
Dim 输入 As 字符串 = 1.9e-3mA
Dim 结果作为 字符串 = Regex.Split (输入, [^。\d - ])( 0 )' 首先添加Imports System.Text.RegularExpressions
这会将输入字符串拆分为不是数字的字符或一个点,然后它需要数组的第一部分来获取数字部分。
使用正则表达式! :笑:
\d +(\.\ + +)?([eE] - ?\d +)?应该做它 - 它只匹配浮点数部分,并忽略其他任何东西。
' Imports System.Text.RegularExpressions
' < span class =code-comment>为Visual Basic构建的正则表达式:星期五,2014年4月11日,01:54:44 PM
' 使用Expresso版本:3.0.3634,http://www.ultrapico.com
'
' 对此的描述正则表达式:
'
' 任意数字,一次或多次重复
' [1]:编号的捕获组。 [\.\d +],零次或一次重复
' \。\\ \\ d +
' Literal。
' 任意数字,一次或多次重复
' [2]:编号的捕获组。 [[eE] - ?\d +],零或一次重复
' [eE ] - ?\d +
' 此类中的任何字符:[eE]
' - ,零或一次重复
' 任意数字,一次或多次重复
'
'
公开 Dim regex As Regex = 新正则表达式(_
\ + +(\。\ d +)?([eE] - ?\d +)?,_
RegexOptions.IgnoreCase _
或 RegexOptions.CultureInvariant _
或 RegexOptions.IgnorePatternWhitespace _
或 RegexOptions.Compiled _
)
' 捕获InputText中的第一个匹配项(如果有)
Dim m As Match = regex.Match(InputText)
Dim result As string = m.Value
IndexOf
或LastIndexOf
在第一个字符串中给出一些其他字符串的位置。
position = databack.IndexOf( mA)
显示mA的起始位置。如果它不存在,IndexOf将返回-1。
假设它始终存在,你可以做
result = databack。子串( 0 ,databack.IndexOf( mA< /跨度>))
Hi All,
I want to make sure I am only reading the numeric part of a response that is coming back as a string say "1.95e-3mA " to get this I been using substring to trim off the last part the darned board has just replied with "1.9e-3mA " meaning my careful
result = dataBack.Substring(0,3)
has broken and now I can see it all falling apart as the reply is now 1.9e (which breaks a calculation) rather than 1.95. Is there a simple way to do this or do I have to rethink the way I am doing this. Would Val() solve my issue?...
Glenn
Val() wont as I need to be a string I wonder Val it to get the number and the convert it back to a string???....
If the numeric part would always be followed by ane
, then you could use String.Split[^] to split the string into an array and then take the first part of the array:
Dim input As String = "1.9e-3mA" Dim result As String = input.Split("e"c)(0)
But you said in your comment that it won't always be ane
, so you can use Regex.Split[^] to split by a regular expression:
Dim input As String = "1.9e-3mA" Dim result As String = Regex.Split(input, "[^.\d-]")(0) ' first add Imports System.Text.RegularExpressions
This will split the input string at the chars that are not a digit or a dot, and then it takes the first part of the array to get the numeric part.
Use a Regex! :laugh:
\d+(\.\d+)?([eE]-?\d+)?Should do it - it matches the floating point number part only, and ignore anything else.
' Imports System.Text.RegularExpressions ' Regular expression built for Visual Basic on: Fri, Apr 11, 2014, 01:54:44 PM ' Using Expresso Version: 3.0.3634, http://www.ultrapico.com ' ' A description of the regular expression: ' ' Any digit, one or more repetitions ' [1]: A numbered capture group. [\.\d+], zero or one repetitions ' \.\d+ ' Literal . ' Any digit, one or more repetitions ' [2]: A numbered capture group. [[eE]-?\d+], zero or one repetitions ' [eE]-?\d+ ' Any character in this class: [eE] ' -, zero or one repetitions ' Any digit, one or more repetitions ' ' Public Dim regex As Regex = New Regex( _ "\d+(\.\d+)?([eE]-?\d+)?", _ RegexOptions.IgnoreCase _ Or RegexOptions.CultureInvariant _ Or RegexOptions.IgnorePatternWhitespace _ Or RegexOptions.Compiled _ ) ' Capture the first Match, if any, in the InputText Dim m As Match= regex.Match(InputText) Dim result As string = m.Value
IndexOf
orLastIndexOf
give the position of some otherstring inside the first string.
position = databack.IndexOf("mA")
shows you where "mA" starts. If it is not present, IndexOf will return -1.
Assuming that it is always present, you could do
result = databack.Substring(0,databack.IndexOf("mA"))
这篇关于如何在字符串中找到字符串,Instr是VB6方式...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!