Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify and remove newline and white spaces?

Tags:

xcode

ios

I am making an nsmutable array by separating a string by component it is causing a lot of new line and white spaces to be inserted in the array how to identify and remove them?

for (int i=0;i<contentsOfFile.count; i++) 
 {
        if(!([[contentsOfFile objectAtIndex:i]isEqual:@"\n"]||[[contentsOfFile     objectAtIndex:i]isEqual:@""]))
       [arrayToBereturned addObject:[contentsOfFile objectAtIndex:i]];
 }

this code which i am using cannot identify all new line charectors thanks

like image 537
amar Avatar asked May 03 '12 06:05

amar


People also ask

How do I get rid of white spaces?

To remove all white spaces from String, use the replaceAll() method of String class with two arguments, i.e. Program: Java.

How do you get rid of the white space between strings?

trim() method removes the leading and trailing spaces present in the string. strip() method removes the leading and trailing spaces present in the string. Also, it is Uniset/Unicode character aware.

What is the best way to remove whitespace characters from the start or end?

In JavaScript, trim() is a string method that is used to remove whitespace characters from the start and end of a string. Whitespace characters include spaces, tabs, etc. Because the trim() method is a method of the String object, it must be invoked through a particular instance of the String class.

How do I remove white space in HTML?

Remove white space using font-size We can also remove white space by setting parent element font-size to 0 and child elements font-size to 17px .


1 Answers

To remove all extra space and \n from your string-

NSString* result = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

than prepare your contentsOfFile Array.

like image 79
Naina Soni Avatar answered Oct 20 '22 04:10

Naina Soni