943,865 Members | Top Members by Rank

Ad:
Apr 22nd, 2009
0

String

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 22nd, 2009
0

Re: String

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>");
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 22nd, 2009
0

Re: String

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
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
Apr 22nd, 2009
0

Re: String

Click to Expand / Collapse  Quote originally posted by JooClops ...
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.

javascript Syntax (Toggle Plain Text)
  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');
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
May 7th, 2009
0

Re: String

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:
JavaScript Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009
May 7th, 2009
0

Re: String

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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
May 7th, 2009
0

Re: String

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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
May 8th, 2009
0

Re: String

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
JooClops is offline Offline
44 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Counting
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript Validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC