• 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: comparing Strings.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: comparing Strings.


  • Subject: Re: comparing Strings.
  • From: Negm-Awad Amin <email@hidden>
  • Date: Tue, 7 Oct 2008 12:32:02 +0200

A littlebit tricky  (probably ugly), but shorter:

NSArray* components = [source componentsSeperatedByCharactersInSet: [NSCharacterSet …]];
NSString* result = [components componentsJoinedByString:@""];


;-)


Cheers

Am Di,07.10.2008 um 11:55 schrieb Graham Cox:


On 7 Oct 2008, at 7:46 pm, Gerriet M. Denkmann wrote:


On 7 Oct 2008, at 09:20, cGraham Cox <email@hidden> wrote:

On 7 Oct 2008, at 4:22 pm, Sandro Noel wrote:

i'm having a problem comparing some type of strings, for example the
one giving me a problem right now.
is let's say in my database i have 4 version of the same string .


"sandro's computer"
"sandro's_computer"
"sandros computer"
"sandros_computer"

I would like them to compare as being equal,
is there a way I can mingle with the comparing routine
to ask it to ignore some characters in the comparison?


First strip out the characters to be ignored:

NSString* tempString = [myString stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"'_ "]];

returns "sandroscomputer" given any of the above.

Are you quite sure?
The Tiger documentation says: "Returns a new string made by removing from both ends of the receiver characters contained in a given character set."


If this is still true, then the internal apostrophs would not get removed.

But maybe I am misunderstanding the documentation.
DId not actually test this.

Whoops, I made a mistake.

I thought I'd seen a method for stripping characters in a set from a string, and searching for it jumped on the stringByTrimming... method. But as you correctly state, that's not right! However, I wasn't going entirely mad, as I had seen the method, courtesy of a handy NSString category by Alastair Houghton. I hope he won't mind my copying it in below.

Apologies for the misleading info.

G.



@implementation NSString (RemovalOfCharsInSet)


/* Remove all characters from the specified set */


- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet options:(unsigned) mask
{
NSRange range;
NSMutableString* newString = [NSMutableString string];
unsigned len = [self length];

mask &= ~NSBackwardsSearch;
range = NSMakeRange (0, len);
while (range.length)
{
NSRange substringRange;
unsigned pos = range.location;

range = [self rangeOfCharacterFromSet:charSet options:mask range:range];
if (range.location == NSNotFound)
range = NSMakeRange (len, 0);

substringRange = NSMakeRange (pos, range.location - pos);
[newString appendString:[self substringWithRange:substringRange]];

range.location += range.length;
range.length = len - range.location;
}

return newString;
}



- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet
{
return [self stringByRemovingCharactersInSet:charSet options:0];
}



- (NSString *) stringByRemovingCharacter:(unichar) character
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithRange:NSMakeRange (character, 1)];

return [self stringByRemovingCharactersInSet:charSet];
}


@end

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

Amin Negm-Awad email@hidden




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: comparing Strings.
      • From: Sandro Noel <email@hidden>
References: 
 >Re: comparing Strings. (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: comparing Strings. (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: apple remote
  • Next by Date: Re: CoreData: Fetching object with maximum of property
  • Previous by thread: Re: comparing Strings.
  • Next by thread: Re: comparing Strings.
  • Index(es):
    • Date
    • Thread