String subtraction
String subtraction
- Subject: String subtraction
- From: Andrew Merenbach <email@hidden>
- Date: Mon, 24 Mar 2003 19:36:52 -0800
What is the best way to subtract characters in a given set from a
string? For example, I might wish in one instance to remove the
letters "e," "o," and "h" from "hello, world," yielding "ll, wrld."
I'm actually porting a program from JavaScript, and the original
string-subtraction code is the following:
function sbtString(s1,s2) {
var ous=""; s1+=""; s2+="";
for (var i=1;i<=s1.length;i++) {
var c1=s1.substring(i-1,i);
var c2=s2.indexOf(c1);
if (c2==-1) {ous+=c1;}
}
return ous;
}
I have modified this to:
int i;
NSRange c2;
NSString *c1;
NSMutableString *ous = [NSMutableString string];
for (i=1;i<=[s1 length];i++) {
NSString *c1 = [s1 substringWithRange:NSMakeRange(i-1,i)];
c2 = [s2 rangeOfString:c1];
if (c2.location==NSNotFound) [ous appendString:c1];
}
(where s1 is the original string, and s2 contains the characters to
remove)--but am receiving range errors, for some reason (I'm probably
missing something obvious--if anyone can point out the problem I'd be
much obliged). (Also, would an NSScanner work more efficiently for
this?)
Take care,
Andrew
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.