How do you get the number of spaces before a string starts in ColdFusion?
I mean, I have a string like this " Hello World!"
I want to get the count of spaces (in this case 3) before the word "Hello" starts.
trim() method trim space at starting and ending of a string by check unicode value '\u0020' (value of space)(most minimum value in unicode is '\u0020' = space) so it check every index from starting until get value greater than space unicode and also check from last until it get value greater than space and trim start & ...
To count the spaces in a string: Use the split() method to split the string on each space. Access the length property on the array and subtract 1. The result will be the number of spaces in the string.
To count the number of leading spaces in Python we have a really handy function called lstrip(). It gives us the output string by removing all the leading spaces present in our string. It also helps in removing defined function arguments present as leading characters in the string.
replace(/ /g,""); var length = newString. length . Just a quick way to guarantee no spaces are there.
I'm not too familiar with ColdFusion but considering this API you should be able to get the result you want with:
Len(str) - Len(LTrim(str))
But maybe there is a better solution :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With