Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow string questions

    1) How can I trim the ends (begin of string & end of string) by definition - let's say I want it not to have the char b at the ends. "bdfgf" will become: "dfgf"; and "bbbbfbbbbbbb" will become "f". Is there a function that does that?
    2) How can I count the repeat of chars in a string. ie: counting the "f" in: "dfsdfhfff" will return 5.

    Thanks.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There are surely many ways to achieve these goals. For the first question, I would suggest using regular expressions in combination with String.replace().

    Code:
    var foo = 'bafzb';
    alert(foo.replace(/^b*(.*?)b*$/, '$1'));
    That deletes the desired character (in this case it's obviously 'b'). For the second question, you could either delete all unwanted characters and count the length of the resulting string, like

    Code:
    alert(foo.replace(/[^b]/g, '').length);
    or you could iterate over the string and check if the current character is ok and if so, increment a counter variable.

    Code:
    var count = 0;
    for (var i = 0; i < foo.length; i++) {
    	if (foo.charAt(i) == 'b') {
    		count++;
    	}
    }
    alert(count);
    De gustibus non est disputandum.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks alot!
    only one question.
    If the char I want to remove with str.replace and REGexp is special - how can I put it in the expression? let's say it's semicolon (...
    Thanks!

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ShMiL...

    How 'bout a couple of new Stringy methods (no user's fee):
    Code:
    <html>
    <head>
    <title>feh!</title>
    </head>
    <body bgcolor="ghostwhite">
    <script type="text/javascript">
    
    String.prototype.trim_from_ends = function(sChar, allCase) {
         var caseflag = (allCase) ? 'i' : '';
         var RElt = new RegExp('^' + sChar + '*', caseflag);
         var RErt = new RegExp(sChar + '*$', caseflag);
         return this.replace(RElt, '').replace(RErt, '');
    }
    
    String.prototype.occurs = function(sChar, allCase) {
         var flags = (allCase) ? 'gi' : 'g';
         var reps = this.match(new RegExp(sChar, flags));
         return (reps != null) ? reps.length : 0;
    }
    
    x = 'bdfgfabcdebbbb';
    document.write('x = <font color="red">"' + x + '"</font>');
    document.write('<br />');
    document.write('occurrances of "b":<font color="red"> ' + x.occurs('b') + '</font>');
    document.write('<br />');
    document.write('trim "b" from ends: <font color="red">"' + x.trim_from_ends('b') + '"</font>');
    document.write('<br /><br />');
    
    x = '%@!hoohah%@!';
    document.write('x = <font color="red">"' + x + '"</font>');
    document.write('<br />');
    document.write('occurrances of "b":<font color="red"> ' + x.occurs('b') + '</font>');
    document.write('<br />');
    document.write('occurrances of "%@!":<font color="red"> ' + x.occurs('%@!') + '</font>');
    document.write('<br />');
    document.write('trim "%@!" from ends: <font color="red">"' + x.trim_from_ends('%@!') + '"</font>');
    document.write('<br /><br />');
    
    x = 'Honey, I shrank the kids, honey';
    document.write('x = <font color="red">"' + x + '"</font>');
    document.write('<br />');
    document.write('occurrances of "honey":<font color="red"> ' + x.occurs('honey') + '</font>');
    document.write('<br />');
    document.write('occurrances of "honey" (case-insensitive):<font color="red"> ' + x.occurs('HONEY',true) + '</font>');
    document.write('<br />');
    document.write('trim "honey" & ", " from ends: <font color="red">"' + x.trim_from_ends('honey').trim_from_ends(', ') + '"</font>');
    document.write('<br />');
    document.write('trim "honey" (case-insensitive) & ", " from ends: <font color="red">"' + x.trim_from_ends('honey',true).trim_from_ends(', ') + '"</font>');
    
    </script>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    436
    Thanks
    1
    Thanked 0 Times in 0 Posts
    When I try this:
    x.occurs('.')
    and it doesn't work
    is there a way to check the number of appearances of DOTs?

    thanks

  • #6
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Metacharacters always give me a headache......here:
    Code:
    <html>
    <head>
    <title>feh!</title>
    </head>
    <body bgcolor="ghostwhite">
    <script type="text/javascript">
    
    String.prototype.trim_from_ends = function(str, allCase) {
         var caseflag = (allCase) ? 'i' : '';
         str = str.replace(/([\$\^\?\*\(\)\+\.\/\[\]\{\}\-])/g, '\\$1');
         var RElt = new RegExp('^' + str + '*', caseflag);
         var RErt = new RegExp(str + '*$', caseflag);
         return this.replace(RElt, '').replace(RErt, '');
    }
    
    String.prototype.occurs = function(str, allCase) {
         var flags = (allCase) ? 'gi' : 'g';
         str = str.replace(/([\$\^\?\*\(\)\+\.\/\[\]\{\}\-])/g, '\\$1');
         var reps = this.match(new RegExp(str, flags));
         return (reps != null) ? reps.length : 0;
    }
    
    x = '$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./$^*()+./'
    alert('x = "' + x + '"\nrun: x.occurs(\\'()\\')\nresult: ' + x.occurs('()'));
    
    x = 'Hello...I grow bananas for banana splits in a Banana republic, amigo...'
    alert('x = "' + x + '"\nrun: x.occurs(\\'banana\\', true)\nresult: ' + x.occurs('banana', true));
    
    x = 'These are metacharacters: "$^*()+[]{}?.-". These are metacharacters: "$^*()+[]{}?.-". These are metacharacters: "$^*()+[]{}?.-". These are metacharacters: "$^*()+[]{}?.-". These are metacharacters: "$^*()+[]{}?.-".'
    alert('x = "' + x + '"\nrun: x.occurs(\\'$^*()+[]{}?.-\\')\nresult: ' + x.occurs('$^*()+[]{}?.-'));
    
    x = '.%@!.hoohah.%@!.';
    alert('x = "' + x + '"\nrun: x.trim_from_ends(\\'.%@!.\\')\nresult: ' + x.trim_from_ends('.%@!.'));
    
    x = '$^*()+[]{}?.- naked $^*()+[]{}?.-';
    alert('x = "' + x + '"\nrun: x.trim_from_ends(\\'$^*()+[]{}?.-\\')\nresult: ' + x.trim_from_ends('$^*()+[]{}?.-'));
    
    </script>
    </body>
    </html>
    Let me know if I missed any...
    Last edited by cheesebag; 09-07-2003 at 10:42 PM.
    &nbsp;........ another wild guess ........


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •