Click here to Skip to main content
14,929,864 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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
Posted

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:
C#
string input = "   2365    2365    ";
string output = Regex.Replace(input, @"(?<=\d+)\s+(?=\d+)", "");

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:
HTML
<!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;
}
</script>

</body>
</html>
   
v2
Comments
R Harshal 25-Jan-14 6:19am
   
Thanks for your Quick reply.
Sorry to say it will not work because everytime it will not the same value as i mentioned that is 2365 2365. It will get on change as you have hardcoded that value which i dont want.the value will be anything as it will be the number,so if any number came i want to remove the space between number.That i required.Kindly guide me.
OriginalGriff 25-Jan-14 6:23am
   
That's just an example, to show you what it means: whatever value you put in the input string the second line will work with. So if you values comes from a textbox:
string output = Regex.Replace(myTextBox.Text, @"(?<=\d+)\s+(?=\d+)", "");
Try it!
R Harshal 25-Jan-14 6:32am
   
Great .Thanks Buddy..
OriginalGriff 25-Jan-14 6:35am
   
You're welcome.
R Harshal 25-Jan-14 8:23am
   
is it possible in Regular Expression using javascript?
Rahul VB 25-Jan-14 14:49pm
   
OG, +5, you are the "MOM"
Requires FrameWork 4.0 or later


... edit 1 ...
The string.Trim() method will not remove any digit characters, but will remove all white-space characaters at the start and end of a string.

The second string.Trim(char[]) method will remove only characters you specify in the parameter array, and string.TrimStart(char[]), and string.TrimEnd(char[]) work the same way.

string.TrimStart, and string.TrimEnd, which require an array of char parameters char[] will not remove any character unless you specify it.

If you want to convert a string to a number, you must remove any leading, or trailing, white-space !

... end edit 1 ...

I like to use the String.Concat operator to clean up strings so they contain only digits:
C#
string allNumericString = String.Concat
(
    YourString.Where(Char.IsDigit)
);
That will get rid of white space of all flavors, etc.

For string cleaning that allows the period to remain:
C#
string allNumericWithDotString = string.Concat
(
    YourString.Where(chr => (Char.IsDigit(chr) || (chr == '.')))
);
Do keep in mind that 'IsDigit is Unicode compatible, so, if you are dealing with Unicode fonts, some combinations of characters may be validated as digits, depending on context.
   
v2
Comments
R Harshal 25-Jan-14 8:23am
   
is it possible in Regular Expression using javascript?
Abhinav S 25-Jan-14 11:00am
   
5.
Rahul VB 25-Jan-14 14:50pm
   
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900