Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-04-2009, 11:02 AM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 5
Question String Functions - inserting a character into a string

Hi (and thanks in advance for any assistance on this!)

I am new to iPhone SDK, but not to programming.

I have searched the iPhone SDK reference docs and Google, etc. and I can’t find the string functions or any examples to do the following:

I would like to insert a character at a specified spot in a string (i.e. 2 characters from the end of the string):


Ex) if you started with the following text “mystring” I would like to insert an underscore character“_” two characters from the end of the string. So that “mystring” would be changed to “mystri_ng

Also, if anyone has a link to a quick reference of string manipulation functions and examples it would be greatly appreciated!

String Length
String Left (string, count)
String Right (string, count)
String Concatenation
Etc.

Thanks!
DragonTech is offline   Reply With Quote
Old 03-04-2009, 11:08 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,049
Default

Quote:
Originally Posted by DragonTech View Post
Hi (and thanks in advance for any assistance on this!)

I am new to iPhone SDK, but not to programming.

I have searched the iPhone SDK reference docs and Google, etc. and I can’t find the string functions or any examples to do the following:

I would like to insert a character at a specified spot in a string (i.e. 2 characters from the end of the string):


Ex) if you started with the following text “mystring” I would like to insert an underscore character“_” two characters from the end of the string. So that “mystring” would be changed to “mystri_ng

Also, if anyone has a link to a quick reference of string manipulation functions and examples it would be greatly appreciated!

String Length
String Left (string, count)
String Right (string, count)
String Concatenation
Etc.

Thanks!
Check out the NSString documentation in xCode. All the methods are there to enable you to manipulate your string...not sure if there is an Insert method but there is definitely substring and appendstring methods that will help with what you are trying to achieve.

Regards, M.
wuf810 is offline   Reply With Quote
Old 03-04-2009, 11:20 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: I'm a Brit-pat.
Posts: 171
Default

Quote:
Originally Posted by DragonTech View Post
String Length
String Left (string, count)
String Right (string, count)
String Concatenation
Etc.
Thanks!
OK, There's a lot of documentation here, that will be of help:
NSString Class Reference

To give you a quick idea (I'm writing on a PC looking at the above docs, so this may need a tweak - but it should still help point you in the right direction)

//Create a string.
NSString *myString = [[NSString alloc] init];
myString = @"some string text.";

//Get string length.
val = [myString length];

//Append strings.
NSString *stringOne = [[NSString alloc] init];
NSString *stringTwo = [[NSString alloc] init];
stringOne = @"some string text.";
stringTwo = @" Some more string text";
stringone = [stringOne stringByAppendingString:stringTwo];

//Clearing up after yourself (very import if you alloc).
[stringOne release];
[stringTwo release];

Hope that helps.
coulls is offline   Reply With Quote
Old 03-04-2009, 11:47 AM   #4 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 5
Default Thanks!

Thanks for the quick replies Wuf810 & Coulls -

this was enough info to get me on the right path. I will post my solution to this example shortly, in case anyone else has a similar question.
DragonTech is offline   Reply With Quote
Old 03-04-2009, 12:00 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: I'm a Brit-pat.
Posts: 171
Default

Quote:
Originally Posted by DragonTech View Post
Thanks for the quick replies Wuf810 & Coulls -

this was enough info to get me on the right path. I will post my solution to this example shortly, in case anyone else has a similar question.
Just had a late thought: If a string needs to be edited after creation, you may need to use NSMutableString instead of NSString.

EDIT: Found your insertion routine:
http://developer.apple.com/documenta...Reference.html
Look for "insertString:atIndex", which allows you to insert one string inside another at a specified position.

Last edited by coulls; 03-04-2009 at 12:02 PM. Reason: See "EDIT:"
coulls is offline   Reply With Quote
Old 03-04-2009, 12:11 PM   #6 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 5
Default Here's a solution

This solution worked in the iPhone simulator 2.2:

NSString *myString = [[NSString alloc] init];
NSString *myStringPrt1 = [[NSString alloc] init];
NSString *myStringPrt2 = [[NSString alloc] init];
NSString *combinedString = [[NSString alloc] init];


myString = @"mystring";

//Get string length
int myStringLen = [myString length];

//Get first part of the string minus the last two characters
myStringPrt1 = [myString substringWithRange:NSMakeRange(0,myStringLen-2)];

//get the last two characters of the string
myStringPrt2 = [myString substringFromIndex:myStringLen-2];

//add the underscore to the end of string part 1
combinedString = [myStringPrt1 stringByAppendingString:@"_"];

//add the last two characters to the string after the underscore
combinedString = [combinedString stringByAppendingString:myStringPrt2];


RESULT: "mystri_ng"

Thanks to all for the help on this. I hope this helps out anyone else trying to do this type of thing.
DragonTech is offline   Reply With Quote
Reply

Bookmarks

Tags
character, functions, inserting, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 233
11 members and 222 guests
ADY, CKAmike, Dani77, Duncan C, HemiMG, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:21 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0