Get a string from a string with length from a location - objective-c

I have a NSString object #"i like to do something".
I want to create a new string from that string with the length of 4 and get from 3rd character.
For example, the result must be #"like".
What can I do in objective-c?

Check out - (NSString *)substringWithRange:(NSRange)range
NSString *string = #"i like to do something";
NSString *substring = [string substringWithRange:NSMakeRange(2, 4)];

The substringWithRange message will do it.
substringWithRange: NSMakeRange (2, 6)

Related

Format string that already has format specifier %#

I have a string which already contains a formatter %#.
NSString *str = #"This is an %#";
I need to parse that string and to replace %# with 'example'. If I use
[NSString stringWithFormat:#"%#", str];
I get the following output:
This is an %#
I want output like:
This is an example
NSString *str = #"This is an %#";
str = [str stringByReplacingOccurrencesOfString:#"%#" withString:#"example"];
I would recommand to use the formatted string as "format"
NSString *str = #"This is an %#";
str = [NSString stringWithFormat:str, #"example"];
is working with every type. A better solution than replacing, because you can use unspecified replacings
is very usefull if you use localized.strings with x values you want to add ;)

Concatenate String in String Objective-c

I want to place a string within a string. Basically in pseudo code:
"first part of string" + "(varying string)" + "third part of string"
How can I do this in objective-c? Is there a way to easily concatenate in obj-c? Thanks!
Yes, do
NSString *str = [NSString stringWithFormat: #"first part %# second part", varyingString];
For concatenation you can use stringByAppendingString
NSString *str = #"hello ";
str = [str stringByAppendingString:#"world"]; //str is now "hello world"
For multiple strings
NSString *varyingString1 = #"hello";
NSString *varyingString2 = #"world";
NSString *str = [NSString stringWithFormat: #"%# %#", varyingString1, varyingString2];
//str is now "hello world"
Variations on a theme:
NSString *varying = #"whatever it is";
NSString *final = [NSString stringWithFormat:#"first part %# third part", varying];
NSString *varying = #"whatever it is";
NSString *final = [[#"first part" stringByAppendingString:varying] stringByAppendingString:#"second part"];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendFormat:#"%# third part", varying];
NSMutableString *final = [NSMutableString stringWithString:#"first part"];
[final appendString:varying];
[final appendString:#"third part"];
NSString * varyingString = ...;
NSString * cat = [NSString stringWithFormat:#"%s%#%#",
"first part of string",
varyingString,
#"third part of string"];
or simply -[NSString stringByAppendingString:]
You would normally use -stringWithFormat here.
NSString *myString = [NSString stringWithFormat:#"%#%#%#", #"some text", stringVariable, #"some more text"];
Just do
NSString* newString=[NSString stringWithFormat:#"first part of string (%#) third part of string", #"foo"];
This gives you
#"first part of string (foo) third part of string"
Iam amazed that none of the top answers pointed out that under recent Objective-C versions (after they added literals), you can concatenate just like this:
#"first" #"second"
And it will result in:
#"firstsecond"
You can not use it with NSString objects, only with literals, but it can be useful in some cases.
simple one:
[[#"first" stringByAppendingString:#"second"] stringByAppendingString:#"third"];
if you have many STRINGS to Concatenate, you should use NSMutableString for better performance

How to remove a section of a string using escape character

I have a set of NSString representing the names of the files in a directory. These names are structured this way:
XXXXXXXXX_YYYY_AAAA.ext
All the sections separated by "_" are of variable length and I would only have the first.
How can I separate the first part from the other?
Find the position of the '_' character, then get a substring 0 through that position. Note that substringToIndex: does not include the character at the index position.
NSRange r = [myString rangeOfString:#"_"];
NSString *res = [myString substringToIndex:r.location];
Take a look at the NSString method componentsSeparatedByString:. That will tokenize a string and return you an array. Something like this:
NSArray *array = [#"XXXXXXXXX_YYYY_AAAA.ext" componentsSeparatedByString:#"_"];
NSString *firstToken = [array objectAtIndex:0];
NSArray *array = [yourString componentsSeparatedByString:#"_"];
NSString *Xs = [array objectAtIndex:0];
Try componentsSeparatedByString: under the heading Dividing Strings.
NSString Docs

Remove last character of NSString

I've got some trouble 'ere trying to remove the last character of an NSString.
I'm kinda newbie in Objective-C and I have no idea how to make this work.
Could you guys light me up?
NSString *newString = [oldString substringToIndex:[oldString length]-1];
Always refer to the documentation:
substringToIndex:
length
To include code relevant to your case:
NSString *str = textField.text;
NSString *truncatedString = [str substringToIndex:[str length]-1];
Try this:
s = [s substringToIndex:[s length] - 1];
NSString *string = [NSString stringWithString:#"ABCDEF"];
NSString *newString = [string substringToIndex:[string length]-1];
NSLog(#"%#",newString);
You can see = ABCDE
NSString = *string = #"abcdef";
string = [string substringToIndex:string.length-(string.length>0)];
If there is a character to delete (i.e. the length of the string is greater than 0)
(string.length>0) returns 1, thus making the code return:
string = [string substringToIndex:string.length-1];
If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
(string.length>0) returns 0, thus making the code return:
string = [string substringToIndex:string.length-0];
which prevents crashes.
This code will just return the last character of the string and not removing it :
NSString *newString = [oldString substringToIndex:[oldString length]-1];
you may use this instead to remove the last character and retain the remaining values of a string :
str = [str substringWithRange:NSMakeRange(0,[str length] - 1)];
and also using substringToIndex to a NSString with 0 length will result to crashes.
you should add validation before doing so, like this :
if ([str length] > 0) {
str = [str substringToIndex:[s length] - 1];
}
with this, it is safe to use substring method.
NOTE : Apple will reject your application if it is vulnerable to crashes.
Simple and Best Approach
[mutableString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];

Get a string with ascii code objective-c

I have a ascii code, for the letter 'a', and I want to get a string by its ascii code, is it possible with NSString?
This could also work:
NSString *foo = [NSString stringWithFormat:#"%c", 97];
Didn’t test it.
If you mean you have a byte that represents an ASCII-encoded character and you want to make a string out of it, NSString has an initializer just for that.
char characterCodeInASCII = 97;
NSString *stringWithAInIt = [[NSString alloc] initWithBytes:&characterCodeInASCII length:1 encoding:NSASCIIStringEncoding];