How can I get word between spaces in UITextView? [duplicate] - ios

This question already has answers here:
NSString tokenize in Objective-C
(9 answers)
Closed 8 years ago.
I need get all the words of a UITextView of individual form.
In an array for example.

Simple.. Just use this if you have one space between two words:
NSArray *words = [textview.text componentsSeparatedByString:#" "];
OR if there is the possibility of multiple spaces between words:
NSArray *words = [textview.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]

Related

How to remove characters from an NSString and replace with spaces? [duplicate]

This question already has answers here:
String replacement in Objective-C
(6 answers)
Closed 8 years ago.
I have a NSString:
NSString *string1 = #"http://example.com/this-is-an-example-post"
By removing a range of characteres I have substringed it to:
NSString *string2 = #"this-is-an-example-post"
But I am struggling to get it into the form of:
NSString *string3 = #"this is an example post"
Any help? Much appreciated.
For example this way
string3 = [string2 stringByReplacingOccurrencesOfString:#"-" withString:#" "];

How to convert NSString to NSMutableString in Objective-C? [duplicate]

This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];

componentsSeperatedByString returns full word [duplicate]

This question already has answers here:
Is there a simple way to split a NSString into an array of characters?
(4 answers)
Closed 9 years ago.
I am trying to get each letter of an NSString using this line of code:
NSArray *array = [string componentsSeparatedByString:#""];
//string is equal to Jake
NSLog(#"Array Count:%d",[array count]);
I am expecting to get each letter of the word "Jake" but instead I am getting the whole word. Why?
From Apple's Doc about this method
NSString *list = #"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:#", "];
produces an array { #"Norman", #"Stanley", #"Fletcher" }.
So empty separator will not separate each character of string, this
method doesn't work this way.
Here is an answer for your question
How to convert NSString to NSArray with characters one by one in Objective-C
The idea of separating a string by nothing doesn't logically make sense, it is like trying to divide by zero.
But to answer the question:
NSMutableArray *stringComponents = [NSMutableArray arrayWithCapacity:[string length]];
for (int i = 0; i < [string length]; i++) {
NSString *character = [NSString stringWithFormat:#"%c", [string characterAtIndex:i]];
[stringComponents addObject:character];
}`

how to convert NSMutableArray into NSString? [duplicate]

This question already has answers here:
How to convert elements of NSMutableArray to NSString
(3 answers)
Closed 9 years ago.
I am trying to convert (or copy?) a NSMutableArray into a NSString. I guess my problem is that I don't really understand the structure of a NSString. In my limited knowledge, a string could look like this: in iphone
Try this code:-
NSString *string = [array componentsJoinedByString:#","];
take the NSMutableString and append every array string into your string
like
string = [string appendString:[NSString stringWithFormat:"%#", [array objectAtIndex:i]]];

How to initialize NSString as text with double quotes [duplicate]

This question already has answers here:
Is it possible to include a quotation mark as part of an nsstring?
(7 answers)
Closed 8 years ago.
i want to initialize NSString is as "hai" with double qoutes..is it possible? any help please?
but NSString *str = #"hai"; i want to convert it as "hai" without direct initialization?any help pls?
If you want to convert an existing string please use this:
NSString *newString = [NSString stringWithFormat:#"\"%#\"",str];
NSString* foo = #"\"hai\"";

Resources