Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring from NSString [closed]

I am using NSString and I want to get a substring of it that contains the first 20 characters of my string. How can I do that?

like image 470
MouzmiSadiq Avatar asked Feb 08 '13 06:02

MouzmiSadiq


People also ask

What is the use of substring method?

Definition and Usage. The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string between "start" and "end", not including "end" itself.

What is a substring in Python?

Definition and Usage. The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

What is an NSString in Java?

An NSString object encodes a Unicode-compliant text string, represented as a sequence of UTF–16 code units. All lengths, character indexes, and ranges are expressed in terms of 16-bit platform-endian values, with index values starting at 0.

Where does the substring start and end in a string?

The substring starts at a specified character position and continues to the end of the string. The zero-based starting character position of a substring in this instance. A string that is equivalent to the substring that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance.


Video Answer


3 Answers

You can use substringToIndex.

NSString *mystring = @"This is a test message having more than 20 characters";
NSString *newString = [mystring substringToIndex:20];
NSLog(@"%@", newString);
like image 165
Bijoy Thangaraj Avatar answered Oct 20 '22 15:10

Bijoy Thangaraj


NSString *str = @"123456789012345678901234";
NSString *subStr = [str substringWithRange:NSMakeRange(0, 20)];
like image 45
Inder Kumar Rathore Avatar answered Oct 20 '22 14:10

Inder Kumar Rathore


NSString *string = @"I am having a simple question.I am having NSString and i want a substring of it that contains first 20 ";
NSString *first20CharString = [string substringToIndex:20];
like image 45
Rushi Avatar answered Oct 20 '22 16:10

Rushi