解决方案 »
- VB的数据类型Integer(2字节)是不是没有方法弄成范围0~65535?
- VBA群 49592594 ,欢迎大家加入。
- 很不解的问题!!
- 谁会用TTS中文发声
- setup factory
- vb和ACCESS数据库连接有几种方法
- 《visual studio 2005技术大全》、《SQL Server 2008基础教程》、《C#与VB.NET网络通信开发实战》三本全新书籍特价转让
- 如何判断一个数能被另一个数整除
- mciSendString("set mp3 Time format hms ", returnstr, 128, 0)我想用它来设定MP3的时间显示格式。返回错误是290。怎么办?刚刚放假回家,又可以和大家一起在上面混。散分!!
- 萌新求助
- vb实现类似xml的字符串的行合并【第二场】
- VB SetWindowPos隐藏窗口问题
从最后一个字符往前搜索,
(1)对结尾的 <num> 和 <who>做标签,(2)往前查找对应的开头标签,
(3)查找到到相同的标签后,
再往前搜索,如果还有一个对应的结尾标签,删除搜索到开头结尾,转向2.
如果找到另外一个结尾标签,转向1.end。
<msg><num>123</num><con>哈哈,你这个笨蛋无奈。。< /con><who>小明</who><msg>??
<msg><num>123</num><con>你这个</con><who>小明</who><msg>你说这些文字‘在一行中’,是不是多了个<msg>啊?
Private Sub Form_Load()
Dim s As String, tmps() As String, i As Byte, tempS As String, n As Long
n = 1
s = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg><msg><num>123</num><con>你这个</con><who>小明</who><msg><msg><num>123</num><con>笨蛋</con><who>小明</who><msg><msg><num>456</num><con>没错!</con><who>芙蓉</who><msg><msg><num>456</num><con>我是猪</con><who>芙蓉</who><msg><msg><num>123</num><con>无奈。。</con><who>小明</who><msg>"
tmps = Split(s, "<msg><msg>")
For i = 0 To UBound(tmps)
Swho = CutString(1, tmps(i), "<who>", "</who>")
If tempS = "" Then
tempS = tmps(i)
Debug.Print tempS
GoTo 5
Else
n = InStr(1, tempS, "<msg><msg>")
End If
If CutString(n, tempS, "<who>", "</who>") = Swho Then
tempS = Left(tempS, InStr(n, tempS, "</con>") - 1) & Right(tmps(i), Len(tmps(i)) - InStr(tmps(i), "<con>") - 4)
Debug.Print tempS
Else
tempS = tempS & "<msg><msg>" & tmps(i)
Debug.Print tempS
5: End If
Next
Debug.Print tempS
End Sub'文本截取函数
Public Function CutString(StartNum As Long, InPutString As String, LeftString As String, RightString As String)
On Error Resume Next
If StartNum = 0 Then StartNum = 1
Dim StrLine As Long, StrLine2 As Long
StrLine = InStr(StartNum, InPutString, LeftString) + Len(LeftString)
StrLine2 = InStr(StrLine, InPutString, RightString)
CutString = Mid(InPutString, StrLine, StrLine2 - StrLine)
End Function
输出结果如上。本代码只对本字符串负责。嘿嘿
<msg><num>123</num><con>你这个</con><who>小明</who><msg>
<msg><num>123</num><con>笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con>没错!</con><who>芙蓉</who><msg>
<msg><num>456</num><con>我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con>无奈。。</con><who>小明</who><msg>
<msg><num>123</num><con>木有代码</con><who>小明</who><msg>↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 要变成 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓<msg><num>123</num><con>哈哈,你这个笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con>没错!我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con>无奈。。木有代码</con><who>小明</who><msg>
Private Sub Command1_Click()
Dim tmp1$, tmp2$, tmp3$, tmp4$, tmp5$, tmp6$, tmp7$, tmp8$
Dim wz1%, wz2%, wz3%, wz4%, s$, i%
s = "<msg><num>123</num><con>哈哈,< /con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 你这个</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>" & _
"<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>" & _
"<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>"
's = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 你这个</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>" & _
"<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>" & _
"<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 木有代码</con><who>小明</who><msg>"
While s <> ""
iSplit s, wz1, wz2, wz3, wz4, tmp1, tmp2, tmp3
tmp7 = Mid(s, 1, wz4)
s = Mid(s, wz4 + 1)
If s <> "" Then
For i = 1 To Len(s)
iSplit s, wz1, wz2, wz3, wz4, tmp4, tmp5, tmp6
If tmp1 = tmp4 And tmp3 = tmp6 Then
tmp2 = tmp2 & tmp5
tmp7 = Mid(s, 1, wz4)
s = Mid(s, wz4 + 1)
If s = "" Then
tmp8 = tmp8 & Mid(tmp7, 1, wz2) & tmp2 & Mid(tmp7, InStr(wz2 + 1, tmp7, "<"))
Exit For
End If
Else
tmp8 = tmp8 & Mid(tmp7, 1, wz2) & tmp2 & Mid(tmp7, InStr(wz2 + 1, tmp7, "<"))
Exit For
End If
Next
Else
tmp8 = tmp8 & tmp7
End If
Wend
MsgBox tmp8
End SubPrivate Sub iSplit(s$, wz1%, wz2%, wz3%, wz4%, tp1$, tp2$, tp3$)
wz1 = InStr(InStr(1, s, ">") + 1, s, ">")
wz2 = InStr(InStr(wz1 + 1, s, ">") + 1, s, ">")
wz3 = InStr(InStr(wz2 + 1, s, ">") + 1, s, ">")
wz4 = InStr(InStr(wz3 + 1, s, ">") + 1, s, ">") tp1 = Trim(Mid(s, wz1 + 1, InStr(wz1 + 1, s, "<") - wz1 - 1))
tp2 = Trim(Mid(s, wz2 + 1, InStr(wz2 + 1, s, "<") - wz2 - 1))
tp3 = Trim(Mid(s, wz3 + 1, InStr(wz3 + 1, s, "<") - wz3 - 1))End Sub
返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置。语法 InStr([start, ]string1, string2[, compare])InStr 函数的语法具有下面的参数:部分 说明
start 可选参数。为数值表达式,设置每次搜索的起点。如果省略,将从第一个字符的位置开始。如果 start 包含 Null,将发生错误。如果指定了 compare 参数,则一定要有 start 参数。
string1 必要参数。接受搜索的字符串表达式。
string2 必要参数。被搜索的字符串表达式。
Compare 可选参数。指定字符串比较。如果 compare 是 Null,将发生错误。如果省略 compare,Option Compare 的设置将决定比较的类型。
设置 compare 参数设置为:常数 值 描述
vbUseCompareOption -1 使用Option Compare 语句设置执行一个比较。
vbBinaryCompare 0 执行一个二进制比较。
vbTextCompare 1 执行一个按照原文的比较。
vbDatabaseCompare 2 仅适用于Microsoft Access,执行一个基于数据库中信息的比较。
返回值如果 InStr返回
string1 为零长度 0
string1 为 Null Null
string2 为零长度 Start
string2 为 Null Null
string2 找不到 0
在 string1 中找到string2 找到的位置
start > string2 0
况且也没有一个根节点root node
<msg><num>123</num><con>你这个</con><who>小明</who><msg>
<msg><num>123</num><con>笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con>没错!</con><who>芙蓉</who><msg>
<msg><num>456</num><con>我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con>无奈。。</con><who>小明</who><msg>
首先定义个数组a(2,n)当然变量你要自己选,我这里就代表下啊
然后将你的代码分成若干份
a=<msg><num> 注意,我写的格式一定是不准的,自己调啊。然后应该是标识部分了吧,b=123 或者 b=456
然后继续是:c=</num><con> 继续下来 D=说过的话(“哈哈”这个位置) E=</con><who> F=小明 或者芙蓉这个位置的内容。G=</who><msg>。位置定义完了之后我们就要使用数组来排列内容了,请继续等待
将信息分别赋值给上面提到的A,B,C,D,E,F,G中
然后对数组A(2,n)进行赋值(这里的2,N你可以自己设定,我只是说声明个数组,并不一定是我写的这样的)
首先进行判断:
如果是按照标识判断就是:声明一个变量存放标识 dim qq
for i=1 to n
qq = b'假如123这里是标识的话
(1)IF qq = b then '当标识=标识的时候
A(i,1)=A(i,1)+ D'记录内容
在这里继续获取下一行的信息
将信息分别赋值给上面提到的A,B,C,D,E,F,G中
然后用GOTO 跳到(1)
endif
'如果不等于的话说明说话的人变了,也就是条件变更,
next i
private void button1_Click(object sender, EventArgs e)
{
MsgCollection mc = new MsgCollection(textBox1.Text); textBox2.Text = mc.Merge().ToString();
}
Msg类,
MsgCollection类
下面贴出winform....C#写的,但其他语言思路都是一样的......
5年没写VB了不会写了抱歉
public class Msg
{
public string num { get; set; }
public string con { get; set; }
public string who { get; set; } public Msg()
{
} public Msg(string strMsg)
{
Regex rxNum = new Regex(@"(?!<num>)\d+(?=</num>)");
num = rxNum.Match(strMsg).Value; Regex rxCon = new Regex(@"(?<=<con>).*(?=</con>)");
con = rxCon.Match(strMsg).Value; Regex rxWho = new Regex("(?<=<who>).+?(?=</who>)");
who = rxWho.Match(strMsg).Value;
} public static Msg operator + (Msg a, Msg b)
{
Msg result = new Msg()
{
num = a.num,
con = a.con + b.con,
who = a.who
}; return result;
} public override string ToString()
{
return string.Format("<msg><num>{0}</num><con>{1}</con><who>{2}</who><msg>", num, con, who);
}
}
public class MsgCollection
{
private List<Msg> msgList = new List<Msg>(); public MsgCollection(string strMsgs)
{
Regex rxMsg = new Regex("<msg>.*?<msg>"); MatchCollection matchs = rxMsg.Matches(strMsgs);
foreach (Match match in matchs)
{
Msg m = new Msg(match.Value);
msgList.Add(m);
}
} public MsgCollection Merge()
{
if (msgList.Count >= 2)
{
int len = msgList.Count;
for (int i = 0; i < len; i++)
{
for (int j=i+1; j<len; j++)
{
if (string.Compare(msgList[i].who, msgList[j].who, StringComparison.CurrentCulture) == 0)
{
msgList[i] = msgList[i] + msgList[j]; msgList.RemoveAt(j);
len -= 1;
}
}
}
} return this;
} public override string ToString()
{
StringBuilder sb = new StringBuilder(); foreach (Msg m in msgList)
{
sb.Append(m.ToString());
} return sb.ToString();
}
}
Open "d:\a.txt" For Input As #1
Open "d:\b.txt" For Output As #2
Dim s As String
Do
Line Input #1, s
s = Trim(s)
s = Replace(s, "<msg>", "")
s = Replace(s, "<num>", "")
s = Replace(s, "</num>", "《")
s = Replace(s, "<con>", "")
s = Replace(s, "</con>", "》")
s = Replace(s, "<who>", "")
s = Replace(s, "</who>", "")
s = Replace(s, "</msg>", "")
Print #2, s
Loop While Not EOF(1)
Close #2
Close #1
End SubPrivate Sub Command2_Click()
Open "d:\b.txt" For Input As #1
Open "d:\c.txt" For Output As #2
Dim s As String
Dim b As Long, e As Long
Dim oldnum As String
Dim oldname As String
Dim s1, s2, s3
Dim ns As String
Do
Line Input #1, s
s = Trim(s)
b = InStr(1, s, "《")
e = InStr(1, s, "》")
s1 = Mid(s, 1, b - 1)
s2 = Mid(s, b + 1, e - b - 1)
s3 = Mid(s, e + 1)
If s1 = oldnum Then
'id没变
ns = ns & s2
Else
If ns <> "" Then
ns = ns & "》" & oldname
Print #2, ns
End If
ns = s1 'num
ns = ns & "《" & s2 'con
oldnum = s1
oldname = s3
End If
Loop While Not EOF(1)
ns = ns & "》" & oldname
Print #2, ns
Close #2
Close #1
End SubPrivate Sub Command3_Click()
Open "d:\c.txt" For Input As #1
Open "d:\d.txt" For Output As #2
Dim s As String
Dim b As Long, e As Long
Dim s1, s2, s3
Dim ns As String
Do
Line Input #1, s
s = Trim(s)
b = InStr(1, s, "《")
e = InStr(1, s, "》")
s1 = Mid(s, 1, b - 1)
s2 = Mid(s, b + 1, e - b - 1)
s3 = Mid(s, e + 1)
ns = "<msg><num>" & s1 & "</num><con>" & s2 & "</con><who>" & s3 & "</who><msg>"
Print #2, ns
Loop While Not EOF(1)
Close #2
Close #1
End Suba.txt
<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>
<msg><num>123</num><con>你这个</con><who>小明</who><msg>
<msg><num>123</num><con>笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con>没错!</con><who>芙蓉</who><msg>
<msg><num>456</num><con>我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con>无奈。。</con><who>小明</who><msg>b.txt
123《哈哈,》小明
123《你这个》小明
123《笨蛋》小明
456《没错!》芙蓉
456《我是猪》芙蓉
123《无奈。。》小明c.txt
123《哈哈,你这个笨蛋》小明
456《没错!我是猪》芙蓉
123《无奈。。》小明d.txt
<msg><num>123</num><con>哈哈,你这个笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con>没错!我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con>无奈。。</con><who>小明</who><msg>
Dim STemp As String
Debug.Print "测试字符串1================================="
'测试串1
STemp = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 你这个</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>"
STemp = STemp & "<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>"
' Debug.Print STemp
Call StringDelay(STemp)
Debug.Print "测试字符串1测试完成"
Debug.Print ""
Debug.Print "测试字符串2================================="
'测试串2
STemp = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 你这个</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>"
STemp = STemp & "<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>"
STemp = STemp & "<msg><num>123</num><con> 木有代码</con><who>小明</who><msg>"
' Debug.Print STemp
Call StringDelay(STemp)
Debug.Print "测试字符串2测试完成"End SubPrivate Sub StringDelay(ByVal STemp As String, Optional ByVal sTrim As Boolean = False)
Dim sTempA() As String Dim I As Integer
Dim sLast As String
Dim SCurrent As String Dim S1 As String
Dim S2 As String
Dim S3 As String Dim sContent As String
Dim sSplit As String
Dim SName As String sSplit = "<msg><num>" S1 = "</num><con>"
S2 = "</con><who>"
S3 = "</who><msg>" If sTrim Then
STemp = Replace$(STemp, " ", "") '是否考虑去空格
End If sTempA = Split(STemp, sSplit) For I = 1 To UBound(sTempA)
SCurrent = Mid$(sTempA(I), 1, InStr(1, sTempA(I), S1) - 1)
If SCurrent = sLast Or sLast = "" Then
sContent = sContent & Mid$(sTempA(I), InStr(1, sTempA(I), S1) + Len(S1), InStr(1, sTempA(I), S2) - InStr(1, sTempA(I), S1) - Len(S2))
SName = Mid$(sTempA(I), InStr(1, sTempA(I), S2) + Len(S2), InStr(1, sTempA(I), S3) - InStr(1, sTempA(I), S2) - Len(S3))
sLast = SCurrent
Else
Debug.Print sSplit & sLast & S1 & sContent & S2 & SName & S3 '这是获取到的字符串
I = I - 1
sContent = ""
sLast = ""
End If Next Debug.Print sSplit & sLast & S1 & sContent & S2 & SName & S3 '最后一次得到的字符串End Sub
测试字符串1=================================
<msg><num>123</num><con>哈哈, 你这个 笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con> 没错! 我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>
测试字符串1测试完成测试字符串2=================================
<msg><num>123</num><con>哈哈, 你这个 笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con> 没错! 我是猪</con><who>芙蓉</who><msg>
<msg><num>123</num><con> 无奈。。 木有代码</con><who>小明</who><msg>
测试字符串2测试完成
123 不是小明吗 怎么变小猪了? 当你是手误了
凑个热闹
Sub ldy()
s = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 你这个</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>" & _
"<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>" & _
"<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>" s = Replace(s, ">", "<")
a = Split(s, "<")
f1 = "<msg><num>"
f2 = "</num><con>"
f3 = "</con><who>"
f4 = "</who><msg>"
For i = 0 To UBound(a) - 16 Step 16
x = a(i + 4)
If x1 = x Then
m = m & a(i + 8)
Else
If x1 <> "" Then
ss = ss & f1 + x1 + f2 + m + f3 + n + f4 & vbCrLf '要不要回车,自己拿主意
End If
m = a(i + 8)
n = a(i + 12)
End If
x1 = x
If i = UBound(a) - 16 Then
ss = ss & f1 + x1 + f2 + m + f3 + n + f4
End If
Next
MsgBox ss
End Sub
s = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 你这个</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>" & _
"<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 无奈。。</con><who>小明</who><msg>"
s = Replace(s, ">", "<")
a = Split(s, "<")
f1 = "<msg><num>"
f2 = "</num><con>"
f3 = "</con><who>"
f4 = "</who><msg>"
For i = 0 To UBound(a) - 16 Step 16
x = a(i + 4)
If x1 = x Then
m = m & a(i + 8)
Else
If i > 0 Then ss = ss & f1 + x1 + f2 + m + f3 + n + f4 & vbCrLf '要不要回车,自己拿主意
m = a(i + 8)
n = a(i + 12)
End If
x1 = x
Next
ss = ss & f1 + x1 + f2 + m + f3 + n + f4
MsgBox ss
End Sub
我的意思是,当换成那样后,他的程序就得出的结果就有误了
回复 #42楼,结果是<msg><num>123</num><con>哈哈, 你这个 笨蛋</con><who>小明</who><msg>
<msg><num>456</num><con> 没错! 我是猪 无奈。。</con><who>芙蓉</who><msg>小明和芙蓉合体啦?(*^__^*)
"<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>"
s = "<msg><num>123</num><con>哈哈,</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 你这个</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 笨蛋</con><who>小明</who><msg>" & _
"<msg><num>456</num><con> 没错!</con><who>芙蓉</who><msg>" & _
"<msg><num>456</num><con> 我是猪</con><who>芙蓉</who><msg>" & _
"<msg><num>123</num><con> 无奈。。</con><who>小明</who><msg>" & _
"<msg><num>123</num><con> 我是假小明</con><who>假小明</who><msg>"
s = Replace(s, ">", "<")
a = Split(s, "<")
f1 = "<msg><num>"
f2 = "</num><con>"
f3 = "</con><who>"
f4 = "</who><msg>"
For i = 0 To UBound(a) - 16 Step 16
X = a(i + 4)
n = a(i + 12)
If x1 = X And n = n1 Then
m = m & a(i + 8)
Else
If i > 0 Then ss = ss & f1 + x1 + f2 + m + f3 + n1 + f4 & vbCrLf '要不要回车,自己拿主意
m = a(i + 8)
End If
x1 = X
n1 = n
Next
ss = ss & f1 + x1 + f2 + m + f3 + n1 + f4
MsgBox ss
End Sub
End Sub