View Full Version : Text searching and comparing...
QuackHead
09-20-2002, 09:21 PM
Ok, I don't know how I'm going to explain this.. but here goes ;)
I have a string of text that can be added to any number of times.
let's say the text string looks like this:
var textString = '54,732,23,53,';
I have a value (let's say 732 for example) that gets passed to the function.
I need to use JavaScript to find out if '732' is in the string.
If it is in the string, I need to take it OUT
if it is not in the string I need it added to the end of the string.
Any ideas?
(I know how to do this with VB code, but I want to be able to do it in JavaScript - is there something like VB's "InStr" or "Mid" functions?)
Thanks in advance
~Quack
Bosko
09-20-2002, 11:25 PM
You can use the .indexOf() and .replace() methods of the string:
<script>
var textString = '54,732,23,53';
function example(arg){
if(textString.indexOf(arg)==-1){
textString=textString+","+arg;
}else{
textString=textString.replace(","+arg,"");
}
alert(textString);
}
</script>
See if this is any help
If variable seek is in the array it is removed, if it is not it is added
<P id="qw"></p>
<P id="qwe"></p>
<script language="javascript">
<!--
seek="732"
newstr=""
a=0
str=new Array()
str[str.length]="54"
str[str.length]="732"
str[str.length]="23"
str[str.length]="53"
for(i=0;i<str.length;i++){
if(str[i]==seek){
newstr=str.splice(i,1)
a=1
}
}
if(a==0){
newstr=str.splice(1,0,seek)
}
qw.innerText=newstr
qwe.innerText=str
// -->
</script>
adios
09-21-2002, 01:42 AM
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
//stick this anywhere//////////////////////////////////////////////
String.prototype.cutPaste = function() {
var a, str = '';
if (this == '') {
for (a=0; arguments[a]; ++a) str += (arguments[a] + ((arguments[a+1]) ? ',' : ''));
return str;
}
var e, el, temparr = this.split(',');
for (a=0; arguments[a]; ++a) {
str = arguments[a]; e = 0;
while (el = temparr[e++]) if (el == str) {
temparr.splice(--e, 1);
break;
}
if (e > temparr.length) temparr[temparr.length] = str;
}
return temparr.join(',');
}
//////////////////////////////////////////////////////////////////
var textString = '54,732,23,53';
alert("textString = '" + textString + "'\ntextString.cutPaste('732')\nresult: '" + textString.cutPaste('732') + "'");
alert("textString = '" + textString + "'\ntextString.cutPaste('99')\nresult: '" + textString.cutPaste('99') + "'");
alert("textString = '" + textString + "'\ntextString.cutPaste('54','732','53')\nresult: '" + textString.cutPaste('54','732','53') + "'");
textString = '';
alert("textString = '" + textString + "'\ntextString.cutPaste('1','2','3')\nresult: '" + textString.cutPaste('1','2','3') + "'");
textString = '1111';
alert("textString = '" + textString + "'\ntextString.cutPaste('54','732','53')\nresult: '" + textString.cutPaste('54','732','53') + "'");
</script>
</head>
<body>
</body>
</html>
piglet
09-21-2002, 12:15 PM
This does exactly what was requested (I think):
I can't remember whether you can do non-matching lookbehinds on Javascript replaces...
<HTML><BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
var textString = '154,732,54,23,53,';
function c(n){
var r = new RegExp("^"+n+",|,"+n+",")
var x = textString.replace(r,",");
var x = x.replace(/^,/,"");
textString=(x!=textString)?x: (textString+n+",")
}
//test it
c(54); alert(textString);
c(154); alert(textString);
c(32); alert(textString);
//-->
</SCRIPT>
</BODY>
</HTML>
and another along the lines of Piglets:
<script>
function getNum(n){
rim ='54,23,732,53'
myReg=new RegExp(n+",?" , "ig")
myReg.test(rim)?rim=rim.replace(myReg,''):rim=rim +","+n
alert(rim)}
</script>
QuackHead
09-21-2002, 04:03 PM
Thanks for all your replies!!! They were a huge help.
~Quack
piglet
09-23-2002, 08:41 AM
Hi Pooh,
If you test with the initial string and test from my example using your function you'll see why I did it the way I did.
Using your function:
rim = '154,732,54,23,53,';
getNum(54);
rim='1732,23,53, '
My version can be slightly simplified if you can do non-matching lookbehind assertions in Javascript Regexp replaces - but I'm not sure that you can.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.