How to get the substring of a string [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get substring of nsstring?
how do I get a substring in iOS?
I am new to iphone.In my project i have a string such as chapter1 but actually i want the substring 1 only how it is possible.if any body know this please help me

Check out the substringFromIndex: or substringToIndex: methods of NSString. You can find the documentation for NSString here.
Ex:
NSString *string = #"Chapter1";
NSString *newString = [string subStringFromIndex: ([string length] - 1)]; // => "1"

Related

How to print text from array with some other string? [duplicate]

This question already has answers here:
Append string with variable
(4 answers)
Shortcuts in Objective-C to concatenate NSStrings
(30 answers)
Closed 5 years ago.
I'm new to Objective-C and I couldn't find a direct solution for my problem where it is explained how to add string behind or in front of an array value .
I try to get some text from an array and add some other string behind or infront of it (will be used in tableviewcell). In Android it's working just with + signs, that's why I couldn't handle it here.
I try to do something like below:
"some text" +[arrayName objectAtIndex:indexPath.row] + "some text"
You need to use stringWithFormat to concatenate those values, or you can use NSMutableString as well
Use this
NSString* finalText = [NSString stringWithFormat:#"some text %# some text",[arrayName objectAtIndex:indexPath.row]];
Another way
NSMutableString* finalString = [NSMutableString stringWithString:#"test Text"];
[finalString appendString:[arrayName objectAtIndex:indexPath.row]];
[finalString appendString:#"test Text 2"];
Short:
[NSString stringWithFormat:#"%#/%#/%#", "some text", [arrayName objectAtIndex:indexPath.row], "some text"];
For further answers see: https://stackoverflow.com/a/510444/9310455

Objective C remove end of string after certain character [duplicate]

This question already has answers here:
Remove Characters and Everything After from String
(2 answers)
Closed 8 years ago.
I can't seem to find the answer to this anywhere. I can do it in c but objective c is difficult.
I want to cut the end of a string after a certain character
so user#example.com will become user (cut at '#')
How do I do this?
This will give you the first chunk of text that comes before your special character.
NSString *separatorString = #"#";
NSString *myString = #"user#example.com";
NSString *myNewString = [myString componentsSeparatedByString:separatorString].firstObject;
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:
You can use a combination of substringToIndex: and rangeOfString: methods, like this:
NSString *str = #"user#example.com";
NSRange pos = [str rangeOfString:#"#"];
if (pos.location != NSNotFound) {
NSString *prefix = [str substringToIndex:pos.location];
}
Notes:
You need to check the location against NSNotFound to ensure that the position is valid.
substringToIndex: excludes the index itself, so the # character would not be included.

Convert NSUInteger to string with ARC [duplicate]

This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

How do you assign characterAtIndex to NSString variable? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've looked at several posts to answer my question but i keep getting errors.
I basically have an NSString that holds 2 characters...
myString = #"ABC";
All I want to do is assign the first character in myString to another NSString variable...
NSString *firstChar;
firstChar = [myString characterAtIndex:0];
I get this error when i do this:
Incompatible integer to pointer conversion assigning to 'unichar *' (aka 'unsigned short *') from 'unichar' (aka 'unsigned short')
What am I doing wrong?
NSString's characterAtIndex: returns a unichar, not a string, so you can't just assign it to another string. You need to create a string from it first:
NSString *myString = #"ABC";
unichar firstChar = [myString characterAtIndex:0];
NSString *string = [NSString stringWithCharacters:&firstChar length:1];
Of course, as DarkDust says, you can also use substringWithRange: to get a string from a subrange of a string:
[myString subStringWithRange:NSMakeRange(0, 1)]; // to get the first character
[myString subStringWithRange:NSMakeRange(1, 2)]; // to get the second and third characters
The characterAtIndex: method returns a unichar (an integer representation of the character). What you want is -[NSString substringWithRange:], like [myString substringWithRange:NSMakeRange(0, 1)];

how to get extension file NSString [duplicate]

This question already has answers here:
Get the extension of a file contained in an NSString
(6 answers)
Closed 9 years ago.
I want get extension (format file from NSString) but I dont know about it please tell me syntax for getting format file from NSString.
NSString *name = #"Xcode-tutorial.pdf"
I want get .pdf format and store in one variable
Use NSStrings pathExtension method:
NSString *fileExtension = [name pathExtension];
Did you look in the documentation? There is a method that does just that:
NSString *ext = [name pathExtension];
NSLog(#"%#", ext);
use this my dear:
NSString *a = [name pathExtension];