Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Apr 2009
Posts: 44
Reputation: JooClops is an unknown quantity at this point 
Solved Threads: 0
JooClops JooClops is offline Offline
Light Poster

String

 
0
  #1
Apr 22nd, 2009
Hello,
I have a little question.
Lets say I have string:
string g="ZOMGGGG";
and a Boolean var:
bool S;

so in C# if I write S=g.contains("GGGG");
and I get true if it contains else false.
How do I use that in JavaScript?
Thank You!

JC
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: String

 
0
  #2
Apr 22nd, 2009
you cant directly as they are two seperate languages. you could output content to the page using a script block somthign like the following

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. string g = "ZOMGGGG";
  2. bool s = g.contains("GGGG");
  3. Response.Write("<script type=\"text/javascript\">alert('" + s + "');</script>");
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: JooClops is an unknown quantity at this point 
Solved Threads: 0
JooClops JooClops is offline Offline
Light Poster

Re: String

 
0
  #3
Apr 22nd, 2009
I mean in a form that i make in Html, i need to check that the user name name value doesn't contain the first name of the person.
so what You wrote instead of the 3rd line(replacing by) I can write return s; ? It's in a function...
so the ".contains" works on javascript?
awesome!
thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: String

 
0
  #4
Apr 22nd, 2009
Originally Posted by JooClops View Post
I mean in a form that i make in Html, i need to check that the user name name value doesn't contain the first name of the person.
so what You wrote instead of the 3rd line(replacing by) I can write return s; ? It's in a function...
so the ".contains" works on javascript?
awesome!
thanks
i think you mis interpreted my response then to check if a string contains a string in javascript you want the following.

  1. function stringContains(string, value) {
  2. // To make this case insensitive comment out the following 2 lines
  3. // string = string.toLowerCase();
  4. // value = value.toLowerCase();
  5. return string.indexOf(value);
  6. }
  7.  
  8. // Then call it with the following
  9. var g = 'ZOMGGGG';
  10. var s = stringContains(g, 'GGGG');
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: JooClops is an unknown quantity at this point 
Solved Threads: 0
JooClops JooClops is offline Offline
Light Poster

Re: String

 
0
  #5
May 7th, 2009
Bumping cause I couldn't get it to work, The function should return -1 if it doesn't contain the string right?
this is what I've done:
  1. function string_contains(string,value)
  2. { string=string.toLowerCase();
  3. value=value.toLowerCase();
  4. return string.indexOf(value);
  5. }
  6.  
  7. if (string_contains(theform.pass.value,theform.first_n.value)!=-1)
  8. {
  9.  
  10. theform.pass.focus();
  11.  
  12. theform.pass.select();
  13.  
  14. newalert(iderror16);
  15.  
  16. return (false);
  17.  
  18. }
what's wrong?
cant i send the first_n as a value??
Last edited by peter_budo; May 9th, 2009 at 12:55 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 867
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 123
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: String

 
0
  #6
May 7th, 2009
Hi JooClops,

With this version you can choose whether the test is case-sensitive or case-insensitive and it also returns a proper boolean (not -1 | index):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" type="text/javascript">
  2. //old English expression: "It's like trying to find a needle in a haystack"
  3. function string_contains(haystack, needle, caseInsensitive){
  4. if(needle.toString() === '') {
  5. return false;
  6. }
  7. if(!caseInsensitive) {
  8. return haystack.toString().indexOf(needle.toString()) >= 0;
  9. }
  10. else {
  11. return haystack.toString().toLowerCase().indexOf(needle.toString().toLowerCase()) >= 0;
  12. }
  13. }
  14. alert( string_contains('ABCDEF', 'N') );//false
  15. alert( string_contains('ABCDEF', 'B') );//true
  16. alert( string_contains('ABCDEF', 'b') );//false
  17. alert( string_contains('ABCDEF', 'b', true) );//true
  18. alert( string_contains('ABCDEF', '') );//false
  19. </script>
caseInsensitive defaults to false (ie case-sensitive) if omitted.

if(needle.toString() === '') { return false; } ensures that an empty string does not test positive (which would otherwise be the case).

toString() guards against errors if non -strings are passed in.

I can't see why theform.first_n.value should not work as along as theform.first_n exists (and is within scope).

Airshow
Last edited by Airshow; May 7th, 2009 at 10:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 867
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 123
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: String

 
0
  #7
May 7th, 2009
Something strange here. I successfully edited my post above then it reverted to unedited version without a refresh or anything BUT the edited version appeared again on qoting it! That's not good behaviour. I will report is as a bug.

This is my edited version. If it is identical to the post above, then please don't blame me - it's the site software.

With this version you can choose whether the test is case-sensitive or case-insensitive and it also returns a proper boolean (not -1 | index):
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript" type="text/javascript">
  2. //old English expression: "It's like trying to find a needle in a haystack"
  3. function string_contains(haystack, needle, caseInsensitive){
  4. if(needle.toString() === '') {
  5. return false;
  6. }
  7. if(!caseInsensitive) {
  8. return haystack.toString().indexOf(needle.toString()) >= 0;
  9. }
  10. else {
  11. return haystack.toString().toLowerCase().indexOf(needle.toString().toLowerCase()) >= 0;
  12. }
  13. }
  14. alert( string_contains('ABCDEF', 'N') );//false
  15. alert( string_contains('ABCDEF', 'B') );//true
  16. alert( string_contains('ABCDEF', 'b') );//false
  17. alert( string_contains('ABCDEF', 'b', true) );//true
  18. alert( string_contains('ABCDEF', '') );//false
  19. </script>
caseInsensitive defaults to false (ie case-sensitive) if omitted.

if(needle.toString() === '') { return false; } ensures that an empty string does not test positive (which would otherwise be the case).

toString() guards against errors if non -strings are passed in.

I can't see why theform.first_n.value should not work as along as theform.first_n exists (and is within scope).

Airshow
Last edited by Airshow; May 7th, 2009 at 11:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: JooClops is an unknown quantity at this point 
Solved Threads: 0
JooClops JooClops is offline Offline
Light Poster

Re: String

 
0
  #8
May 8th, 2009
Awesome, it works
thanks Airshow!
btw ""It's like trying to find a needle in a haystack"
I know this expression

thx again.
JC
Last edited by JooClops; May 8th, 2009 at 7:47 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC