• 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
[Newbie] Extracting a specific character number from a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Newbie] Extracting a specific character number from a string


  • Subject: [Newbie] Extracting a specific character number from a string
  • From: Phil Faber <email@hidden>
  • Date: Wed, 6 Aug 2008 23:23:16 +0100

I'm trying to learn more about string manipulation and want to be able to extract a single character from a string. I think I need to use subStringWithRange (exacting from and to the same character location) but I'm not sure and I'm also unsure how to code it.

Basically I'm after the equivalent of the old-fashioned BASIC command MID$ [ i.e. MID$("This is the sentence to check",4,1) to get character 4. ]

The following code works fine except for the..

NSLog(@"Character %i of %@ is %@",i,pass1,[pass1 substringWithRange: 1,2]);

...line simply because I don't know how to code that line. I think it's something to do with how to set the range that I'm getting wrong.

Can anyone please advise what than line is supposed to be to pull out the correct character?

Thanks.

Phil


{
NSMutableString *pass1;
int i;

pass1=@"This is the sentence to check";

NSLog(@"length of %@ is %i",pass1,[pass1 length]);

for (i=1;i<=[pass1 length];i++)
{
// This displays characters UP to character 'i'
NSLog(@"Characters up to %i of %@ are %@",i,pass1,[pass1 substringToIndex:i]);

// This is SUPPOSED to display character number 'i' ONLY
NSLog(@"Character %i of %@ is %@",i,pass1,[pass1 substringWithRange:i,i]);
}



} _______________________________________________

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: [Newbie] Extracting a specific character number from a string
      • From: "Kyle Sluder" <email@hidden>
    • Re: [Newbie] Extracting a specific character number from a string
      • From: Ricky Sharp <email@hidden>
  • Prev by Date: Re: handling idle events
  • Next by Date: Re: [Newbie] Extracting a specific character number from a string
  • Previous by thread: Re: Rotate NSString... I give up :( [SOLVED]
  • Next by thread: Re: [Newbie] Extracting a specific character number from a string
  • Index(es):
    • Date
    • Thread