将多个空格替换为一个? [英] Replacing multiple spaces into just one?

查看:42
本文介绍了将多个空格替换为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在开发这个 VB.NET 程序,我已经尝试使用 .replace() 来执行此操作,但这不是执行此操作的最佳方法.我有一个字符串,在我不想要的数据之间有多个空格,从字符串中去除空格但 1 的最佳方法是什么?

Okay, so I am working on this VB.NET program and I have tried using the .replace() to do this but it's not the best way to do this. I have a string with multiple spaces in between data which I don't want, what would be the best way to strip the spaces from the string but 1?

推荐答案

使用正则表达式匹配多个空格并替换为单个空格:

Use a regular expression to match multiple spaces and replace with a single space:

s = Regex.Replace(s, " {2,}", " ")

这篇关于将多个空格替换为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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