Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string replacing with numbers 1

Status
Not open for further replies.

adamroof

Programmer
Nov 5, 2003
1,107
US
I need help doing exact matches on a string. I think my string.replace is removing more than it should, sometimes. All the values i am storing in an array are numbers, i think thats the issue maybe.

I have two listboxes, and code to move the items from the first one over to the second one. I am recording the second one as the selected options.

I am storing the selected options in a textbox. Now when someone REMOVES an item, it updates the textbox removing the value that was removed.

Code:
for (var i = 0; i < from.options.length; i++) {
    var o = from.options[i];
    if (o.selected) {
        if (!hasOptions(to)) { var index = 0; } else { var index = to.options.length; }
        to.options[index] = new Option(o.text, o.value, false, false);
        [b]listing.value = checkOptionSavedValues(listing.value, o.value);[/b]
    }
}

function checkOptionSavedValues(curVal, newVal) {
    if (curVal.match(newVal)) {
        curVal = curVal.replace(newVal, "");
    }
    else {
        curVal += newVal + ";";
    }
    return curVal.replace(";;", ";");
}

Once the form is submitted, i will be using the textbox value in a string split on the semicolon.

Overall its working ok one by one, and many multiples, but there is a situation when all is selected, then removed, it leaves some partials.

heres the results from a select all (or adding all one by one)
Code:
16;57;17;8;79;4;77;11;95;18;19;76;83;40;75;23;22;87;62;90;74;91;26;60;39;92;28;70;73;44;88;30;31;12;33;34;81;35;59;78;86;13;

here's the remaining results from remove all
Code:
;8;0;89;90;
Notice, that 89 is not even in the above list!

Thanks for any insight you can provide.
 
[tt]function checkOptionSavedValues(curVal, newVal) {
//curVal is allowed to end with or without ";"
var rx=new RegExp("\\b"+newVal+"\\b;?","g");
var s=curVal;
if (rx.test(curVal)) {
s = s.replace(rx, "");
}
return s;
}[/tt]
 
That seemed to do it! Only thing i had to add was the string appending else statement that builds the additional selections. It worked pretty good, no rogue semicolons were left either after a remove all which eliminated the need for my replace(";;",";") as well!

function checkOptionSavedValues(curVal, newVal) {
//curVal is allowed to end with or without ";"
var rx = new RegExp("\\b" + newVal + "\\b;?", "g");
var s = curVal;
if (rx.test(curVal)) {
s = s.replace(rx, "");
}
else {
s += newVal + ";";
}

return s;
}
 
I see, I had forgotten that part. You're right.

In that construction, it will always end up with semi-colon. On the server-side, the split would have to do in conjunction of eliminating the last entry by design. (Sticking to it, the regex can use simply ; without the quantifier ?.)
 
server side is use this...

string[] selected = txtSelected.Text.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

that can make ;65;45;35; into 3 items (65 45 35) automagically instead of the 5 you would think.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top