napsternapster Posted October 16, 2009 Share Posted October 16, 2009 morning Developers, I'm currently finalizing a project today but the problem I came across is the number of spaces when a user entering data.I need a code to remove trailing and spaces between words like the following example. # = number of spaces ##Hellow#####world## Want to Accomplish: Remove the spaces(#) before Hellow and after world, Eliminate all unnecessary spaces(#) between the words leaving one space(#) between the two worlds. Thanking you in advice. Quote Link to comment Share on other sites More sharing options...
kickstart Posted October 16, 2009 Share Posted October 16, 2009 Hi Something like this:- function getRidOfSpaces(InStr) { InStr = String.trim(InStr); var lastLength = InStr.length + 1; while(InStr.length < lastLength) { lastLength = InStr.length; InStr = InStr.replace(' ',' '); } return InStr; } This just loops through converting 2 spaces to a single space until the length doesn't change doing this. All the best Keith Quote Link to comment Share on other sites More sharing options...
salathe Posted October 16, 2009 Share Posted October 16, 2009 You could also do something like: var subject = " Hello World "; var trimmed = subject.trim().replace(/ +/g, ' '); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.