• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: String subtraction
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String subtraction


  • Subject: Re: String subtraction
  • From: Jeff Disher <email@hidden>
  • Date: Tue, 25 Mar 2003 00:23:59 -0500

Try NSMutableString's "replaceOccurrencesOfString:withString:options:range:" method. It should do exactly what you want but you will probably have to run it once for each character which could be costly if this code needs to be fast.
You could also use NSScanner and then set omitted characters with "setCharactersToBeSkipped:" and then you should be able to use "scanString:intoString:" to get what you are looking for. That is probably better than the above method I suggested.

I haven't tried either but both should work.

Hope that helps,
Jeff.


On Monday, March 24, 2003, at 10:36 PM, Andrew Merenbach wrote:

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.


Jeff Disher
President and Lead Developer of Spectral Class
Spectral Class: Shedding Light on Innovation
http://www.spectralclass.com/
_______________________________________________
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.

  • Follow-Ups:
    • RE: String subtraction
      • From: "Jonathan E. Jackel" <email@hidden>
References: 
 >String subtraction (From: Andrew Merenbach <email@hidden>)

  • Prev by Date: String subtraction
  • Next by Date: Re: Location and Size of Main Menu --- Are They Defined Anywhere
  • Previous by thread: String subtraction
  • Next by thread: RE: String subtraction
  • Index(es):
    • Date
    • Thread