另一种格式化字符串的方法 [英] Another way to format string

查看:60
本文介绍了另一种格式化字符串的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




来自字符串f.e. AA - BB-C ----乙 - AAADEE 我需要得到输出 2A2BCB3AD2E



下面的代码不那么好,但我想知道在没有太多IF条件的情况下是否有更简单的方法来实现这一点?



谢谢



昏暗TZ作为字符串=

尺寸PZ作为字符串=

尺寸Zn作为整数= 1个

尺寸代码作为字符串= TextBox1中。文字

Dim print As String = Nothing



i = 1到Len(代码)

tz =中(代码,i,1)

如果tz<> - 。然后

。如果PZ = TZ然后

的Zn的Zn = + 1个

,否则

。如果PZ< ;> 那么

如果zn> 1然后

print = print& zn& pz

zn = 1

否则

print = print& pz

结束如果

结束如果

结束如果

pz = tz

结束如果

下一页

如果zn> 1然后

print = print& zn& pz

tz = 1

否则

print = print& PZ

结束如果

TextBox2.Text =打印

解决方案

好了,给你一些提示......(虽然我看不到破折号和输出之间的任何关系)



你可以简单地使用标准字符串函数来分割每个破折号上的字符串遗漏空字符串。然后你可以简单地循环遍历数组并计算所有相同的字符并输出它们。



查看标准字符串函数以查看它们可以做些什么来帮助你。



祝你好运!


Hi
From string f.e. "AA--BB-C----B--AAADEE" I need to get output "2A2BCB3AD2E"

The code below does that well, but I was wondering if there is a simpler way to achieve this without too much IF conditions?

Thanks

Dim tz As String = ""
Dim pz As String = ""
Dim zn As Integer = 1
Dim code As String = TextBox1.Text
Dim print As String = Nothing

For i = 1 To Len(code)
tz = Mid(code, i, 1)
If tz <> "-" Then
If pz = tz Then
zn = zn + 1
Else
If pz <> "" Then
If zn > 1 Then
print = print & zn & pz
zn = 1
Else
print = print & pz
End If
End If
End If
pz = tz
End If
Next
If zn > 1 Then
print = print & zn & pz
tz = 1
Else
print = print & pz
End If
TextBox2.Text = print

解决方案

Well, to give you some tips... (although I don't see any relation between the dashes and the output)


You could simply use standard string functions to split the string on each dash and leave out the empty strings. Then you can simply loop through the array and count all the same characters and output these.


Look into the standard string functions to check out what they can do to help you out.


Good luck!


这篇关于另一种格式化字符串的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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