dim str as string str=replace(text1.text,vbcrlf,"")
Do While 0 < InStr(Form1.Text1.Text, vbCrLf & vbCrLf) Form1.Text1.Text = Replace(Form1.Text1.Text, vbCrLf & vbCrLf, vbCrLf) Loop Form1.Text1.SelStart = Len(Form1.Text1.Text): Form1.Text1.SetFocus
您的意思是这样吗?Dim str1() As String Dim str2 As String str2 = Replace(txt1.Text, vbCrLf, "") str1 = Split(str2, vbNewLine)如果是这样,不行啊,本来是一行一行的字符串,变成连起来了
1楼老师的意思 Dim ss$ s = Split(Form1.Text1.Text, vbCrLf) For i = 0 To UBound(s) If Trim(s(i)) <> "" Then ss = ss & s(i) & vbCrLf Next i Form1.Text1.Text = ss Form1.Text1.SelStart = Len(Form1.Text1.Text): Form1.Text1.SetFocus
这个不适合我的,呵呵,因为我下面还要用到这句:For t1 = 0 To UBound(str1)所以必须要用数组,呵呵
try: '去除后面的回车符 function GetText(s as string) as string s=trim(s) while instreev(s,vbnewline)=len(s)-1 s=mid(s,1,Len(s)-2) wend GetText=s end function '调用 Dim str1() As String
Dim s$, ss$(), k&s = Text1 Do k = InStr(s, vbCrLf & vbCrLf) s = Replace(s, vbCrLf & vbCrLf, vbCrLf) Loop While k > 0 ss = Split(s, vbCrLf) For k = 0 To UBound(ss) '...... Next
用Replace(txt1.Text, " ", "")是有危险性的,它替换了txt1中的所有空格,不仅仅是字符串末尾的空格...改下GetText即可: function GetText(s as string) as string s=trim(s) while instrrev(s,vbnewline)=len(s)-1 s=rtrim(mid(s,1,Len(s)-2)) wend GetText=s end function
str=replace(text1.text,vbcrlf,"")
Form1.Text1.Text = Replace(Form1.Text1.Text, vbCrLf & vbCrLf, vbCrLf)
Loop
Form1.Text1.SelStart = Len(Form1.Text1.Text): Form1.Text1.SetFocus
您的意思是这样吗?Dim str1() As String
Dim str2 As String
str2 = Replace(txt1.Text, vbCrLf, "")
str1 = Split(str2, vbNewLine)如果是这样,不行啊,本来是一行一行的字符串,变成连起来了
Dim ss$
s = Split(Form1.Text1.Text, vbCrLf)
For i = 0 To UBound(s)
If Trim(s(i)) <> "" Then ss = ss & s(i) & vbCrLf
Next i
Form1.Text1.Text = ss
Form1.Text1.SelStart = Len(Form1.Text1.Text): Form1.Text1.SetFocus
这个不适合我的,呵呵,因为我下面还要用到这句:For t1 = 0 To UBound(str1)所以必须要用数组,呵呵
'去除后面的回车符
function GetText(s as string) as string
s=trim(s)
while instreev(s,vbnewline)=len(s)-1
s=mid(s,1,Len(s)-2)
wend
GetText=s
end function
'调用
Dim str1() As String
text1=GetText(txt1.Text) '处理一下
str1 = Split(txt1.Text, vbNewLine)
msgBox ubound(str1)
Do
k = InStr(s, vbCrLf & vbCrLf)
s = Replace(s, vbCrLf & vbCrLf, vbCrLf)
Loop While k > 0
ss = Split(s, vbCrLf)
For k = 0 To UBound(ss)
'......
Next
sorry:
instree笔误了,应该是instrrev8楼的代码意思是自己定义一个处理字符串结尾多余回车符函数
其中用到了VB内置函数instrrev,它是从字符串结尾处开始查找指定字符这儿就是查找vbnewline...
Dim idx1 As Integer
Dim str1() As String
Dim str2() As String
Dim p&
Dim q&
Do
p = InStr(txt1.Text, vbCrLf & vbCrLf)
txt1.Text = Replace(txt1.Text, vbCrLf & vbCrLf, vbCrLf)
Loop While p > 0
Do
q = InStr(txt2.Text, vbCrLf & vbCrLf)
txt2.Text = Replace(txt2.Text, vbCrLf & vbCrLf, vbCrLf)
Loop While q > 0str1 = Split(txt1.Text, vbNewLine)
str2 = Split(txt2.Text, vbNewLine)
Dim t1 As Integer
Dim t2 As Integer
For t1 = 0 To UBound(str1)
t1 = t1 + 1
Next
For t2 = 0 To UBound(str2)
t2 = t2 + 1
Next
If t1 <> t2 Then
MsgBox "行数不一样,重来!", vbOKOnly, "提示"
Exit Sub
Else
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\db.mdb;" & "Persist Security Info=False"
conn.Open
For idx1 = 0 To UBound(str1)
str1(idx1) = Trim(str1(idx1))
str1(idx1) = Replace(str1(idx1), " ", "")
str1(idx1) = Replace(str1(idx1), " ", "")
str1(idx1) = Replace(str1(idx1), Chr(0), "")
str1(idx1) = Replace(str1(idx1), Chr(63), "")
str1(idx1) = Replace(str1(idx1), "?", "")
str2(idx1) = Trim(str2(idx1))
str2(idx1) = Replace(str2(idx1), " ", "")
str2(idx1) = Replace(str2(idx1), " ", "")
str2(idx1) = Replace(str2(idx1), Chr(0), "")
str2(idx1) = Replace(str2(idx1), Chr(63), "")
str2(idx1) = Replace(str2(idx1), "?", "")
Dim sql As String
sql = "insert into student(姓名,分数) values ('" & str1(idx1) & "'," & Val(str2(idx1)) & ")"
conn.Execute sql
Next
conn.Close
Set conn = Nothing
End If
当txt1 中有空行时,会出错,说什么“下标越界”
真的都不会出错了呀,效果很好啊,呵呵小弟都试过了,只有当一种情况时有状况发生,比如txt1有 2行字符 + 一行空格 + 一空行,而txt2只有两行字符,这时会先提示“行数不一样”,接着再点一下按钮就会成功小弟就把那句改成:txt1.Text = GetText(Replace(txt1.Text, " ", ""))
不会有问题吧,有点钻牛角尖了呵呵
text1=replace(text1,chr(13),"") 清除回车符
text1=replace(text1,chr(10),"") 清除换行符
function GetText(s as string) as string
s=trim(s)
while instrrev(s,vbnewline)=len(s)-1
s=rtrim(mid(s,1,Len(s)-2))
wend
GetText=s
end function