|
-
Nov 20th, 2001, 05:55 AM
#1
Thread Starter
Evil Genius
javascript trim() equivalent?
as the title - what is the equivalent of the trim() statement from vb in javascript please ?
-
Nov 20th, 2001, 07:13 AM
#2
PowerPoster
hi
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);
-
Nov 20th, 2001, 07:17 AM
#3
Thread Starter
Evil Genius
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
-
Nov 20th, 2001, 07:47 AM
#4
Perhaps you can use this code for it. Just replacing all the spaces with nothing.
Code:
var sString = 'Hallo ik ben Gek';
var sResult = '';
var sReplace = / /g;
// removing all the spaces
sResult = sString.replace(sReplace,'');
-
Nov 20th, 2001, 08:25 AM
#5
PowerPoster
hi
Tell me also when you find it
-
Nov 20th, 2001, 09:35 AM
#6
Frenzied Member
But if you just want to replace the leading and trailing whitespace...
Code:
var myString = " I have Mojo Foo ";
// trim myString
myString.replace(/^\s+|\s+$/g, "");
Wow... that was simple.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Nov 23rd, 2001, 09:48 AM
#7
Thread Starter
Evil Genius
This works great thanks !!!! 
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 ?
-
Nov 26th, 2001, 10:59 AM
#8
Frenzied Member
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.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Nov 26th, 2001, 11:10 AM
#9
Thread Starter
Evil Genius
Thanks for taking the time to explain that - helped me a lot (& I'll get that book )
-
Nov 27th, 2001, 06:34 AM
#10
PowerPoster
hi
Though that is not a function, but a great line of code!!!!
So tell me how to use it.
-
Nov 27th, 2001, 09:33 AM
#11
Frenzied Member
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.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Nov 14th, 2002, 12:12 PM
#12
PowerPoster
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, ""))
-
Nov 14th, 2002, 01:20 PM
#13
Addicted Member
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.
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.
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Nov 14th, 2002, 01:21 PM
#14
PowerPoster
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.
I tried both ways
-
Nov 14th, 2002, 02:09 PM
#15
Addicted Member
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>
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Nov 14th, 2002, 02:13 PM
#16
PowerPoster
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
}
}
-
Nov 14th, 2002, 02:14 PM
#17
PowerPoster
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
}
}
-
Nov 14th, 2002, 02:24 PM
#18
Addicted Member
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
}
}
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Nov 14th, 2002, 02:28 PM
#19
PowerPoster
-
Nov 14th, 2002, 02:33 PM
#20
Addicted Member
Sorry. This is what I used to test it. Perhaps I did something wrong when I adapted it to your function.
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>
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.
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().
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|