C#的字符串每隔4位加一个‘-’号

C#的字符串每隔4位加一个‘-’号,第1张

string str="BFEBFBFF000306A914BF9154"

str=System.Text.RegularExpressions.Regex.Replace(str,@"岩唯型(\w{4})","$1-"粗猜).Trim('山卖-')

Private Sub Form_Load()

xstr = ""咐陵 '新拆档字符串

ystr = "0000000000" '原字符串

For n = 1 To Len(ystr)

If n Mod 4 = 0 Then

xstr = xstr + Mid(ystr, Len(ystr) - n + 1, 1) + "旅简乱,"

Else

xstr = xstr + Mid(ystr, Len(ystr) - n + 1, 1)

End If

Next

xstr = StrReverse(xstr)

End Sub

思路:输入一串字裂亩符,for循环字符串肆侍森的长度,每循环一次截取一个,当截取到4个了就加“,”,当字符串刚刚能够被4整除就不加“,”,实谈数现代码如下:

Console.WriteLine("请输入一串字符:")

string str = Console.ReadLine()

Console.WriteLine("按4个分:")

string new_str = ""

for (int i = 0i <str.Lengthi++)

{

new_str += str.Substring(i, 1)

if ((i + 1) % 4 == 0&&(i+1)!=str.Length)

{

new_str += ","

}

}

Console.WriteLine(new_str)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/bake/11990569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存