如何在字符串中找到字符串,Instr是VB6方式... [英] How Can I find a String within a String, Instr was the VB6 way...

查看:439
本文介绍了如何在字符串中找到字符串,Instr是VB6方式...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想确保我只是读取一个响应的数字部分,它会以字符串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 an e, 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 an e, 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 or LastIndexOf 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆