本人不才,初学VB,难以飞越上述高山,求高人指点。
问题是这样子的,我要从文件中调一个记事本入textbox(多行),然后再用串口发送坐标值给单片机。现在的问题就是从记事本中的字符提取X和Y后面的数字出来。
如:N0001 G01 X100.0 Y50.0 F1
    N0002 G00 X50.0 Y50.0
(1)现在知道G、X、Y、F后面的数字,怎样提取?
(2)提取完成后,如何换到下一行再提取数字?

解决方案 »

  1.   

    用mid函数逐个字符提取出来;用isnumeric函数判断是否数字。
      

  2.   

    字母出现的顺序是固定的,还是随机的.也就是说是不是每行都固定为:N0001 G01 X100.0 Y50.0 F1 
    这样,只是字母后面的数字不同而已啊????
      

  3.   

    line=split( textbox1.text,vbcrlf)
    for i=0 to ubound(line)
    num= split(line(i)," ")
    for j=0 to ubound(num)
      select left(num(j),1)
          case "N"
               N=  mid(num(i),2)
    ......
      end select
    next
    next
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  4.   

    dim s() as string
    Open YOURTEXT.txt For Input As #1 
    Do While Not EOF(1) 
       Line Input #1, str 
       s()=split(str," ")
       for i=0 to ubound(s)
          if left(str,1)="G" then
             str=trim(s(i))
             l=len(str)
             g=val(right(str,l-1))
         end if
          if left(str,1)="X" then
             str=trim(s(i))
             l=len(str)
             x=val(right(str,l-1))
         end if
          if left(str,1)="Y" then
             str=trim(s(i))
             l=len(str)
             y=val(right(str,l-1))
         end if
          if left(str,1)="F" then
             str=trim(s(i))
             l=len(str)
             f=val(right(str,l-1))
         end if
       next i
       *你的发送g,x,y,f值的代码
    Loop 
      

  5.   

    恩 如果你的文本的规律都是你写出来的这样的话,用4F的split函数是最方便的
      

  6.   

    谢谢各位啦
    文本里的内容:
    (N0001G01X100.0Y50.0F1 
     N0002G00X50.0 Y50.0
    ……
     ……) 
    内容里的格式是很顺便输入的,行于行用回车分开,每行的内容中间可能没有空格号,也可能有。除了N跟G 后面的数字是固定4位跟2位,X,Y,F后面的数字位数是未定的,也有可能是负数。
      

  7.   

    str=line(i)ist=instr(1,str,"N")
    iEd=instr(ist,str,"G")
    N=mid(str,Ist+1,iEd-(Ist+1))
      

  8.   

    Option ExplicitFunction GetVal(ByVal Text As String, ByVal ID As String) As Double
        Dim i As Long, V As Double
        
        i = InStr(1, Text, ID)
        If i <> 0 Then
            V = Val(Mid$(Text, i + 1))
        End If    GetVal = V
    End FunctionPrivate Sub Command1_Click()
        Dim aLines() As String, i As Long
        Dim X As Double, Y As Double
        
        aLines = Split(Text1, vbCrLf)
        For i = 0 To UBound(a)
            X = GetVal(aLines(i), "X")
            Y = GetVal(aLines(i), "Y")
            
            Debug.Print X, Y
        Next
    End Sub
      

  9.   

    我也喜欢用val,不过用Val处理这样的字符串要慎重,我有过教训了,如下测试:
    debug.? val("123.03 e5")
    debug.? val("123.03 d10")
      

  10.   

    MSDN:
    浮点数值可表示为 mmmEeee 或 mmmDeee ,其中 mmm 是假数,而 eee 是指数(以 10 为底的幂)。Single 数据类型的最大正数值为 3.402823E+38,或 3.4 乘以 10 的 38 次方;Double 数据类型的最大正数值是 1.79769313486232D+308 或 1.8 乘以 10 的 308 次方。用 D 将数值文字中的假数部分和指数部分隔开,就会导致将该值作为 Double 数据类型来处理。同样,用这种方式使用 E,也会导致将该值作为 Single 数据类型来处理。所以,如果楼主数据中有D加数字或E加数字,可能还是分组处理好...如果没有,instr+val应该效率最好...
      

  11.   

    <theAube angAlveole="24.0" appaireASave="" calage="-99999.0" classe="" codeFabrication="" codeForge="" codeVendor="B1316" coteL="0.0" masse="86.84" momentA="0.0" momentR="3172.2652000000003" momentT="0.0" numAlveole="11" posOrigine="100" referenceAube="338-108-304-0" referenceGE="" serialAube="" typeAube="1" validated="true" /> 
      <theAube angAlveole="26.4" appaireASave="" calage="-99999.0" classe="" codeFabrication="" codeForge="" codeVendor="B1316" coteL="0.0" masse="86.78" momentA="0.0" momentR="3170.0734" momentT="0.0" numAlveole="12" posOrigine="9" referenceAube="338-108-304-0" referenceGE="" serialAube="" typeAube="1" validated="true" /> 
      <theAube angAlveole="28.799999999999997" appaireASave="" calage="-99999.0" classe="" codeFabrication="" codeForge="" codeVendor="B1316" coteL="0.0" masse="86.73" momentA="0.0" momentR="3168.2469" momentT="0.0" numAlveole="13" posOrigine="57" referenceAube="338-108-304-0" referenceGE="" serialAube="" typeAube="1" validated="true" /> 
    怎么提取numAlveole=和posOrigine=   请高手联系我QQ810902123    具体联系   谢
      

  12.   

    <theAube   angAlveole= "24.0 "   appaireASave= " "   calage= "-99999.0 "   classe= " "   codeFabrication= " "   codeForge= " "   codeVendor= "B1316 "   coteL= "0.0 "   masse= "86.84 "   momentA= "0.0 "   momentR= "3172.2652000000003 "   momentT= "0.0 "   numAlveole= "11 "   posOrigine= "100 "   referenceAube= "338-108-304-0 "   referenceGE= " "   serialAube= " "   typeAube= "1 "   validated= "true "   />   
        <theAube   angAlveole= "26.4 "   appaireASave= " "   calage= "-99999.0 "   classe= " "   codeFabrication= " "   codeForge= " "   codeVendor= "B1316 "   coteL= "0.0 "   masse= "86.78 "   momentA= "0.0 "   momentR= "3170.0734 "   momentT= "0.0 "   numAlveole= "12 "   posOrigine= "9 "   referenceAube= "338-108-304-0 "   referenceGE= " "   serialAube= " "   typeAube= "1 "   validated= "true "   />   
        <theAube   angAlveole= "28.799999999999997 "   appaireASave= " "   calage= "-99999.0 "   classe= " "   codeFabrication= " "   codeForge= " "   codeVendor= "B1316 "   coteL= "0.0 "   masse= "86.73 "   momentA= "0.0 "   momentR= "3168.2469 "   momentT= "0.0 "   numAlveole= "13 "   posOrigine= "57 "   referenceAube= "338-108-304-0 "   referenceGE= " "   serialAube= " "   typeAube= "1 "   validated= "true "   />   
    怎么提取numAlveole=和posOrigine=后面对应的数字           请高手联系我QQ810902123         具体联系       谢