用“_"替换每个奇怪的空格 [英] Replace every odd occurrence of space with “_”

查看:61
本文介绍了用“_"替换每个奇怪的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串,我应该用 _ 替换每个奇数出现的空格.

I have the following string where I should replace every odd occurrence of space with _.

字符串:

901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 
908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr

预期字符串:

901_R 902_M 903_Picture_message 904_NA 905_F 906_Local_Relay 907_46 
908_51705 909_306910001112/TYPE=PLMN@mms.cosmote.gr

我尝试计算空间计数并使用正则表达式出现,但未能达到目标.

I tried taking the space count and also using regular expression occurrence but was not able to hit the mark.

推荐答案

循环并保留找到的空格数,但只对奇数起作用:

Loop and keep a count of spaces found but only act on odd numbers:

Dim thing: thing = "901 R 902 M 903 Picture_message 904 NA 905 F 906 Local_Relay 907 46 908 51705 909 306910001112/TYPE=PLMN@mms.cosmote.gr"
Dim i, counter

For i = 1 To Len(thing)
    If Mid(thing, i, 1) = " " Then
        counter = counter + 1
        If counter Mod 2 Then thing = Left(thing, i - 1) & "_" & Mid(thing, i + 1)
    End If
Next

msgbox thing

这篇关于用“_"替换每个奇怪的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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