Find and delete particular text from label XCode - ios

Ok so I am looking for a piece of code that will take text out of label. Before I tell you , I looked all around internet.
Example
Label's text : Hi I am asking for help.
Ok so what I want the program to do is when I hit a button to remove part of the text like this.
From : Hi I am asking for help to Hi I asking for help
deleting the word 'am'.
Don't tell me to do any manual changing like, label.text = #"Hi I asking for help".
Because the value I am trying to change is static and it is an RSS Reader.
Summary :
I want to remove particular text from label, example taking a word out from the middle of the label and re-displaying the new value.

NSString *myString = #"Hi I am asking for help.";
NSString *updated = [myString stringByReplacingOccurrencesOfString:#" am" withString:#""];
NSLog(#"%#",updated);
output will be:- Hi I asking for help.

try this :
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
...to get a new string with a substring replaced (See NSString documentation for others)
Example:
NSString *str = #"Hi I am asking for help.";
str = [str stringByReplacingOccurrencesOfString:#"am"
withString:#""];

Related

Separate a string into different Array

I have a string coming from server , i want to show some part of string in a view and rest part of string in other view .
Thanks for help.
i want to get the last word of last line of first view. i have searched ,but nothing seems to help me. If any one can suggest me something i would be glad to him/her.
NSString *fromIndex = [_categoryWiseNews.article substringWithRange:NSMakeRange(0, lastWordOfFirstView)];
i have tried with substringWithRange but with this you have to know the lastWordOfFirstView index number for breaking the string into two parts, for which i have no idea.
I really don't know what it is that you're asking lastWordOfFirstView might make sense to you because it is relevant to the thing you are working on. But to us it means nothing.
You can separate a string into words in an array doing this...
NSString *string = #"Hello world, this is a string";
NSArray *array = [string componentsSeparatedByString:#" "];
// array is now... [#"Hello", #"world,", #"this", #"is", #"a", #"string"];
Then you can get the last word from it...
NSString *lastWord = [array lastObject];
Use this to get the last word from a sentence:
NSString *lastWord = [[fullSentenceString componentsSeparatedByString:#" "] lastObject];

How to remove line spaces in UITextView in ios

I have a textView, in that I am entering the data and I pressed on the enter it is going to next line and I entered some other data and I saved it.
The result I am getting is :
And it is stored in services like :
{"PersonID":9416,"PeriodID":59130,"Notes":"tyyyttyt
ttrtrtrt
ttttrtrtr","PeriodDate":"2015-05-08T10:50:00"}
But my requirement is that the string should be stored as :
tyyyyttyt ttrttrt ttttrtrtr
I searched in google but I didn't get answer for this.
Can any one please help me to sort out this problem ?
I am very new to Objective C topics.
To remove new line from your string use following code:
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];
where myString is the text you fetch from your UITextView.
Hope this helps!
Separate your string in components separated by any new line character, then join them again to a new string, separating them with a space:
NSString *cleanString = [[myTextField.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#" "];
Try it. Replace Double Space with Single space.
for (int i=0; i<[self.textView.text length]; i++)
{
self.textView.text=[self.textView.text stringByReplacingOccurrencesOfString:#" " withString:#" "];
}

how to split sentence and get specific nsstring

I want one sentence in NSString object like this:
NSString *myWords = #"Hi,I'm Janatan! I love objective C!!!";
I want split this sentence to more part for example :
first part : Hi
second part : I'm Janatan
third part : I love objective C
I want do this with loop but I don't know how to do it. I want split words until arrive to signs (! , ? . and etc)
please guide me about that.
You can do it with the componentsSeparatedByCharactersInSet, e.g.:
NSString *myWords = #"Hi,I'm Janatan! I love objective C!!!";
NSArray *arr = [str componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"!,"]];

iOS modify string to have no line spaces

I am pulling tweets from Twitter and using this to convert it into an array:
NSArray *feedData = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];
Some of the tweets have a large number of new lines and white space, which has become increasingly annoying for the sake of layout.
I want to turn any strings that have multiple lines into a single lined string.
Here is an example string that I need to convert:
I used #ECSliding library with my project but i can’t used with
#UITableView plz provide me the way if u know it.
#kbegeman
& thank u ;
As you can see this mention has a bunch of white space and couple extra lines.
I have tried using this:
NSString *tweet = [currentTweet objectForKey:#"text"];
NSString *trimmedTweet = [tweet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
And I also tried this:
myString = [myString stringByReplacingstringByReplacingOccurrencesOfString:#"\n" withString:#""];
myString = [myString stringByReplacingstringByReplacingOccurrencesOfString:#" " withString:#""];
Unfortunately, this does not work. Does it have something to do with the way I am reading in the JSON? Am I using that method wrong? Is there any other solution anyone can think of? Any help would be great, thanks!
That method stringByTrimmingCharactersInSet trims chars from the start and end of the receiver. You'll probably want something like:
NSString *trimmedTweet = [tweet stringByReplacingOccurrencesOfString:#"\n" withString:#" "];

Replacing occurrences of strings in Objective-C

I'm fetching data from a sqlite database in my iOS program. What I get from the DB is then placed in a UITextView. An example of text that is fetched is:
"Hello this is a example. It has "" double quotes "" and ends with simple"
I want to eliminate the single one, and replace the double by singles. But It's not working. Here is my code:
NSString *question;
question = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement1,1)] copy];
question = [question stringByReplacingOccurrencesOfString:#"\"\"" withString:#"$$"];
question = [question stringByReplacingOccurrencesOfString:#"\"" withString:#""];
question = [question stringByReplacingOccurrencesOfString:#"$$" withString:#"\""];
What is wrong?
The code looks okay; I tried it by doing
NSString *question = #"\"Hello this is a example. It has \"\" double quotes \"\" and ends with simple\"";
question = [question stringByReplacingOccurrencesOfString:#"\"\"" withString:#"$$"];
question = [question stringByReplacingOccurrencesOfString:#"\"" withString:#""];
question = [question stringByReplacingOccurrencesOfString:#"$$" withString:#"\""];
and it worked fine (well, except for the fact that there are extra spaces next to where the quotes used to be, but it's hard to know what you want there)
If you add some
NSLog(#"question is now: %#", question);
lines you'll probably be able to tell what's going on or if not you'll at least have some more information to post!

Resources