as the title - what is the equivalent of the trim() statement from vb in javascript please ?
Printable View
as the title - what is the equivalent of the trim() statement from vb in javascript please ?
I dont think there is any !!!
But this would work:
function trimspaces(word12)
{
word12 = word12 + " ";
var returnword = "";
len1 = word12.length;
flag = 0;
for(ctr = 0 ; ctr<len1;ctr++)
{
mchr = word12.charAt(ctr);
if(mchr != " " || flag == 1 )
{
returnword = returnword + mchr;
flag = 1;
}
}
var newword = "";
word12 = returnword;
len1 = word12.length;
flag = 0;
for(ctr = len1-1 ; ctr >= 0 ;ctr--)
{
mchr = word12.charAt(ctr);
if(mchr != " " || flag == 1 )
{
newword = mchr + newword ;
flag = 1;
}
}
word12 = newword;
newword = ""
len1 = word12.length;
var mchr1 = "";
for(ctr = 0 ; ctr <len1 ;ctr++)
{
mchr = word12.charAt(ctr);
if(mchr != " " || mchr1 != " " )
{
newword = newword + mchr;
}
mchr1=mchr;
}
return(newword);
}
Usage :
word = trimspaces(mvalue);
Thanks for that John - I'm gonna have to ring MS about your other Q also ;)
As for this one, thanks, but I have used one before (there is an equivalent) I just can't think of it at the moment :rolleyes:
Perhaps you can use this code for it. Just replacing all the spaces with nothing. :cool:
Code:var sString = 'Hallo ik ben Gek';
var sResult = '';
var sReplace = / /g;
// removing all the spaces
sResult = sString.replace(sReplace,'');
Tell me also when you find it
But if you just want to replace the leading and trailing whitespace...
Wow... that was simple.Code:var myString = " I have Mojo Foo ";
// trim myString
myString.replace(/^\s+|\s+$/g, "");
This works great thanks !!!! :DCode:/^\s+|\s+$/g
Can someonbe explain these parts though - I knowwhat the replace bit does, but can't find any of hese in my books, what do each parts of this statement mean please ?
Sure, not a problem, though there is an O'Reilly RegEx book that might help, the Rhino (JavaScript) book, which has these listed, and the best: The Llama (Perl) book.
The two /'s are meant to quote a regular expression. You should be able to use any character for this, as long as you use the same character in both places, and don't confuse the parser by using the same character in the expression. If you do you will have to escape it. Avoiding escaping / is why you would want to use something other than /, but now I digress.
The trailing g means greedy. It tells the expression to match as many times as possible.
Okay, inside the expression we have two seperate expressions, joined by a union (or). The | is the logical or operator.
The expression on the left means "whitespace that occurs one or more times at the start of the expression". The \s is the escape sequence for white space characters (space or tab). The + is a modifier. It means "one or more times". The ^ is an operator that marks the beginning of the expression.
On the right side we see the \s+ again, which means "whitespace characters that appear one or more times". But instead of the caret marking the begining of the expression, we have the $ marking the end of the expression. The right side now means "whitespace that occurs one or more times at the end of the expression".
Note: /\s+$|^\s+/g would be the same thing. Don't assume you have to put the clause that must match the beginning of the expression first. That is just done for readability, the or clause doesn't care who is when. Mind however, that if you don't use the greedy flag, then these two expression could be different since your expression will only match once for the expression (either at the beginning or the end of the expression depending on the order the or is evaluated in).
This is case sensitive. \S actually means any non-whitespace character.
Thanks for taking the time to explain that - helped me a lot (& I'll get that book :D )
Though that is not a function, but a great line of code!!!!
So tell me how to use it.
What is not a function, and what do you need help with?
Being ignorant is one thing, but being vague in your ignorance is just plain stupid.
I tried this code and it did NOT remove the spaces
Code:var strTestStringForSpaces = document.form.txtOther9.value
//trim
strTestStringForSpaces.replace(/^\s+|\s+$/g, "")
alert(strTestStringForSpaces.replace(/^\s+|\s+$/g, ""))
I so don't understand that quote. I have no idea what I was saying there.Quote:
Originally posted by CiberTHuG
What is not a function, and what do you need help with?
Being ignorant is one thing, but being vague in your ignorance is just plain stupid.
J4U, it did work. But you are trying to print out the return value from the replace operation, not the value of the string after the replace.
I tried both waysQuote:
Originally posted by Travis G
I so don't understand that quote. I have no idea what I was saying there.
J4U, it did work. But you are trying to print out the return value from the replace operation, not the value of the string after the replace.
Sorry, I was thinking the replace method would change the value of the string. You were right to call the return of the replace, not the string after the replace.
Odd though, but this code works.
Code:<html>
<head>
<title>Obvious Factor</title>
</head>
<body>
<script type="text/javascript">
var strTestStringForSpaces = " Foo Bar Blah ";
alert("|" + strTestStringForSpaces + "|");
alert("|" + strTestStringForSpaces.replace(/^\s+|\s+$/g, "") + "|");
</script>
<p>Done.</p>
</body>
</html>
thanks but I am trying it in this scenario and it doesn't execute
Code:function textarea1_onblur() {
//clears the checkbox when there is just a space in there.
var strTestStringForSpaces = document.form.textarea1.value
strTestStringForSpaces.replace(/^\s+|\s+$/g, "")
if (strTestStringForSpaces == ""){
document.form.textarea1.value = ""
document.form.chkOther.checked = false
}
}
got it
Code:function textarea1_onblur() {
//clears the checkbox when there is just a space in there.
var strTestStringForSpaces = document.form.textarea1.value
strTestStringForSpaces = strTestStringForSpaces.replace(/^\s+|\s+$/g, "")
if (strTestStringForSpaces == ""){
document.form.textarea1.value = ""
document.form.chkOther.checked = false
}
}
You could do this:
Code:function textarea1_onblur() {
//clears the checkbox when there is just a space in there.
var strTestStringForSpaces = document.form.textarea1.value
if (strTestStringForSpaces.search(/\S/) != -1){
document.form.textarea1.value = ""
document.form.chkOther.checked = false
}
}
sorry didn't work
Sorry. This is what I used to test it. Perhaps I did something wrong when I adapted it to your function.
Now that I look at it, maybe you want == in the if, not !=. Sorry. I'm not exactly sure what you are trying to do with your stuff. I wasn't paying that much attention to the periphery.Code:<html>
<head>
<title>Obvious Factor</title>
</head>
<body>
<script type="text/javascript">
var strTestStringForSpaces = " ";
alert("|" + strTestStringForSpaces + "|");
if(strTestStringForSpaces.search(/\S/) != -1) {
alert("Non-empty string");
}
</script>
<p>Done.</p>
</body>
</html>
I know it isn't a browser issue, though. I tested it on the world's worst browser, so I'm sure all others can handle search().