Private Sub Command1_Click()
Dim s As String, s1 As String
s = InputBox("l")
Text1.Text = ""
Open App.Path + "\wls.txt" For Input As #1
While Not EOF(1)
Input #1, s1
If s = Left(s1, Len(s)) Then
If Len(Text1.Text) < 255 Then
Text1.Text = Text1.Text + s1
Else
Text1.Text = Left(Text1.Text, 255)
End If
End If
Wend
Close #1
End Sub
这段代码是计算以输入拼音为开头的拼音后文字的个数的代码,但是我的这段代码没法去掉字符和空格也把他们计算在内了
我输入a则应该显示以a开头所以拼音后面的文字,但是文字的个数不超过255,但是现在文字只有171字符数是207计空格的是254也就是代码把拼音和空格都计算在内了,现在我想去掉拼音和空格占的位置只计算文字

解决方案 »

  1.   

    用Replace将空格替换为空再计算长度
      

  2.   

    function LeftX(byval Text as string, byval MaxLength as long) as string
        dim lLength as long
        dim i as long
        if len(text) <= MaxLength then
            LeftX = Text
            Exit Function
        end if
        for i=1 to Len(Text)
            select case asc(mid$(Text,i,1))
                case 0 to 255
                case else
                    lLength = lLength +1
                    if lLength >= MaxLength then exit for
            end select
        next
        LeftX = Left(Text, i)
    end sub'先将所有的字符串都拼起来,最后用下面的方式保留255个文字
    Text1.Text = LeftX(Text1.Text)
      

  3.   

    你是否只计算汉字?LenB(strConv(Text1, vbFromUnicode)) - Len(Text1)
      

  4.   

    Text1.Text = LeftX(Text1.Text)这行怎么加在end后面了不是不能加在end之后吗?end sub不是函数应该end function 有点错误
      

  5.   

    LenB(strConv(Text1, vbFromUnicode)) - Len(Text1)替换了s = Left(s1, Len(s))这句之后只是把空格去掉了并没有去掉字符也就是拼音
      

  6.   

    从最基本的思路考虑,要去除空格和字符。方法很多,至少有:一、用replace进行枚举式替换;二、用for 循环,读取每一个字符,对字符按asc值 进行判断并过滤。这样,对新得到的变量进行文字统计,即是你要的值。
      

  7.   

    给了你2楼的函数不会用吗?
    Private Sub Command1_Click()
        Dim s As String, s1 As String
        s = InputBox("l")
        Text1.Text = ""
        Open App.Path + "\wls.txt" For Input As #1
        While Not EOF(1)
            Input #1, s1
            If s = Left(s1, Len(s)) Then
                Text1.Text = Text1.Text & s1 & vbCrLf
            End If
        Wend
        Close #1
        Text1.Text = LeftX(Text1.Text, 255)
    End Sub
      

  8.   

    判断ASCII码大于128或者小于0 的就是中文