Replace multiple whitespaces with single whitespace in JavaScript string
Something like this: var s = ” a b c “; console.log( s.replace(/\s+/g, ‘ ‘) )
Something like this: var s = ” a b c “; console.log( s.replace(/\s+/g, ‘ ‘) )
You can do the following using array_map: $new_arr = array_map(‘trim’, explode(‘,’, $str));
The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning: >>> ‘ hello world!’.lstrip() ‘hello world!’ Edit As balpha pointed out in the comments, in order to remove only spaces from the beginning of the string, lstrip(‘ ‘) should be used: >>> ‘ hello world with 2 spaces and a … Read more
String.Trim() returns a string which equals the input string with all white-spaces trimmed from start and end: ” A String “.Trim() -> “A String” String.TrimStart() returns a string with white-spaces trimmed from the start: ” A String “.TrimStart() -> “A String ” String.TrimEnd() returns a string with white-spaces trimmed from the end: ” A String … Read more
The second option really isn’t the same as the others – if the string is “///foo” it will become “foo” instead of “//foo”. The first option needs a bit more work to understand than the third – I would view the Substring option as the most common and readable. (Obviously each of them as an … Read more
Why not just use substring… string.substring(0, 7); The first argument (0) is the starting point. The second argument (7) is the ending point (exclusive). More info here. var string = “this is a string”; var length = 7; var trimmedString = string.substring(0, length);
The substr() function will probably help you here: $str = substr($str, 1); Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.
Taken from this answer here: https://stackoverflow.com/a/5691567/251012 – (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet { NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet] options:NSBackwardsSearch]; if (rangeOfLastWantedCharacter.location == NSNotFound) { return @””; } return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive }
As of R 3.2.0 a new function was introduced for removing leading/trailing white spaces: trimws() See: Remove Leading/Trailing Whitespace
Here’s how you remove all the whitespace from the beginning and end of a String. (Example tested with Swift 2.0.) let myString = ” \t\t Let’s trim all the whitespace \n \t \n ” let trimmedString = myString.stringByTrimmingCharactersInSet( NSCharacterSet.whitespaceAndNewlineCharacterSet() ) // Returns “Let’s trim all the whitespace” (Example tested with Swift 3+.) let myString = … Read more