...

View Full Version : string questions



ShMiL
07-10-2003, 10:45 AM
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.

mordred
07-10-2003, 12:28 PM
There are surely many ways to achieve these goals. For the first question, I would suggest using regular expressions in combination with String.replace().



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



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.



var count = 0;
for (var i = 0; i < foo.length; i++) {
if (foo.charAt(i) == 'b') {
count++;
}
}
alert(count);

ShMiL
07-10-2003, 03:17 PM
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!

cheesebagpipe
07-10-2003, 10:58 PM
ShMiL...

How 'bout a couple of new Stringy methods (no user's fee:D):


<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>

ShMiL
09-07-2003, 09:29 PM
When I try this:
x.occurs('.')
and it doesn't work
is there a way to check the number of appearances of DOTs?

thanks

cheesebag
09-07-2003, 10:40 PM
Metacharacters always give me a headache...:confused:...here:


<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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum