如何删除数字之间的空格 [英] How Do I Remove The Space Between Numbers
问题描述
你好团队
在C#编码中,
i得到的价值就像2365 2365
所以我想删除数字之间的空格.it应该是23652365.i在C#中替换方法和修剪方法之间混淆。如果我使用修剪方法它删除空间但它删除了我不需要的空格之前的数字。我想删除数字之间的空格。我该怎么办?请帮助。
谢谢
Harshal Raut
Hi Team
In C# coding,
i am getting the value as like 2365 2365
so i want to remove the space between numbers.it should be 23652365.i am confuse between Replace method and Trim Method in C#.If i use trim method it remove the space but it remove the number before the space which i dont need.i want to remove the space between the numbers.How should i do this?Kindly help.
Thanks
Harshal Raut
推荐答案
使用String.Replace或String.Trim很难删除数字之间的空格,因为它们是非常愚蠢的方法:它们只是寻找一个字符串并替换它 - 它们并不关心它在字符串中的位置。 br />
相反,试试一个正则表达式:
It's difficult to remove the space between numbers with String.Replace or String.Trim, because they are remarkably dumb methods: they just look for a string and replace it - they don't care where in the string it is.
Instead, try a Regex:
string input = " 2365 2365 ";
string output = Regex.Replace(input, @"(?<=\d+)\s+(?=\d+)", "");
这将删除数字组之间的所有空格并单独留下目的。
使用javascript进行正则表达式是否可行?
是 - 只需使用javascript替换方法:
Which will remove all the spaces between groups of numbers and leave the ends alone.
"is it possible in Regular Expression using javascript?"
Yes - just use the javascript Replace method:
<!DOCTYPE html>
<html>
<body>
<p>Click the button!</p>
<p id="demo"> 1234 5678 </p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/(\d+)(\s+)(\d+)/,"
1
3);
document .getElementById( demo).innerHTML=res;
}
< / 脚本 >
< / body >
< / html >
3"); document.getElementById("demo").innerHTML=res; } </script> </body> </html>
这篇关于如何删除数字之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!