How to remove line spaces in UITextView in ios - 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:#" "];
}

Related

stringWithFormat returning newline

I am adding objects to a mutable array by selecting choice(s) from my table view and viewing them in a text field. When I use stringWithFormat, the line of code is automatically adding in characters.
Example: I choose Bob from my table view
Bob
When I do a NSLog, it is actually appearing as
(
Bob
)
But what is appearing in the text field is
( Bob)
Because there is
(\n Bob\n)
Here is the code that I am using to rid of the parentheses and replace commas with semicolons.
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
// NSString
combinedForces = [ourForces stringByReplacingOccurrencesOfString:#"," withString:#";"];
// NSString
twoCombindForces = [combinedForces stringByReplacingOccurrencesOfString:#"(" withString:#""];
// NSString
UltmateCombinedForces = [twoCombindForces stringByReplacingOccurrencesOfString:#")" withString:#""];
//personnel is the text field
personnel.text = UltmateCombinedForces;
Question now is : What is a less messy path to get
( Bob)
to appear as
Bob
in my text field?
Solution update: After the following lines:
// All Names is the mutable array I am adding information in. tableData is another mutable array with set names in them
[AllNames addObject:tableData[0]];
// If I chose the first option
// NSString
ourForces = [NSString stringWithFormat: AllNames[0]];
Include the following line of code:
personnel.text = [AllNames componentsJoinedByString:#";"];
That got rid of the (\n Bob\n) extra characters that were showing up in the field. Thank you all for you help and wisdom. =)
Use componentsJoinedByString: after filling the AllNames array:
personnel.text = [AllNames componentsJoinedByString:#";"];
OK, I think I understand what you're trying to do now.
You have an array of names...
NSArray *names = #[#"Bob", #"Sam", #"Dave"];
And you want a string of all these names...
#"Bob; Sam; Dave"
You seem to be separating them with a semi colon though? ; Is that correct?
You can do this with...
NSString *nameList = [names componentsJoinedByString:#"; "];
But I'm not entirely sure that this is what you're trying to achieve.
In NSLog the output expression
(
Bob
)
represents an array. To get rid of the parentheses and newlines instead of
NSLog(#"%#", AllNames[0]);
write
NSLog(#"%#", AllNames[0][0]);
… and please use variable names starting with a lowercase letter.
You can use NSCharacter Set to remove items you don't want.. The below might offer a less messy solution.
NSString *originalText = #"( Bob) ";
NSMutableCharacterSet *unwantedChars = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[unwantedChars addCharactersInString:#"()"];
NSString *refinedText = [[originalText componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString:#""];

Removing Specific Characters from NSString

For example if i had a string like
NSString *myString = #"A B C D E F G";
and I want to remove the spaces, and get a string out like "ABCDEFG".
I could use
NSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];
However, for my application I am loading in phone numbers from the address book
and the numbers often have a different formatting layout.
I'm wondering if I have a phone number stored in a string like
+1-(937)673-3451 how would I go about removing only the first "1" the "+" the "-" and the "(" ")".
Overall, I would like to know if it is possible to remove the first "1" without removing the last one in the string?
There are a lot of ways to do this. Here's one:
NSString *phoneNumber = #"+1-(937)673-3451";
NSCharacterSet *removalCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+()-"];
NSArray *components = [phoneNumber componentsSeparatedByCharactersInSet:removalCharacterSet];
NSString *simplifiedPhoneNumber = [components componentsJoinedByString:#""];
NSRange firstCharacterRange = NSMakeRange(0, 1);
if ([[simplifiedPhoneNumber substringWithRange:firstCharacterRange] isEqualToString:#"1"]) {
simplifiedPhoneNumber = [simplifiedPhoneNumber stringByReplacingCharactersInRange:firstCharacterRange withString:#""];
}
NSLog(#"Full phone number: %#", phoneNumber);
NSLog(#"Simplified phone number: %#", simplifiedPhoneNumber);
But really you want to use a library that knows what a phone number is supposed to look like, like libPhoneNumber.

Find and delete particular text from label XCode

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:#""];

How do I easily strip just a comma from an NSString?

I have a UILabel with the following text:
Medium, Black
What I intended to do was grab the words in the string and insert each into a mutable array so I could use each title later on to identify something.
Here's how I done this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByString: #" "] mutableCopy];
NSString *size = [chosenOptions objectAtIndex:0];
NSString *colour = [chosenOptions objectAtIndex:1];
I've logged these two NSString and size is returning "Medium," and colour is correctly returning "Black".
My comparison result is always false because of the comma:
itemExists = [[item colour] isEqualToString:colour] && [[item size] isEqualToString:size] ? YES : NO;
That comma causes itemExists to always equal NO.
Would appreciate a simple solution in code please.
The solution needs to only strip commas and not other characters. When dealing with clothing sizes for females I use sizes in a string like this: "[8 UK]" so remove non-alphanumeric characters would remove these. So I really need a solution to deal with just the commas.
Thanks for your time.
Rather than splitting on spaces, you could split on spaces or commas, like this:
NSMutableArray *chosenOptions = [[[[cell tapToEditLabel] text] componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#" ,"]] mutableCopy];
[chosenOptions removeObject:#""];
This would eliminate commas from the size and colour strings.
[yourString stringByReplacingOccurrencesOfString:#"," withString:#""];
easy squeezy lemon peesey
Try this:
NSString * myString = #"Medium, Black";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#", " withString:#""];
NSLog(#"%#xx",newString);

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:#" "];

Resources