Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

drf1229

macrumors regular
Original poster
Jun 22, 2009
237
0
Say I have an NSString that looks like this:
Code:
NSString *mystring=@"Hello \nThis is a line break \n And this is another one";
I want to use NSString's stringByReplacingOccurrencesOfString:withString: to remove the line breaks but I'm not sure how to do this. I tried using "\n" as the first parameter but got no luck. Any help is greatly appreciated!
 
@"\n" :)

Code:
data=[data stringByReplacingOccurrencesOfString:@"\n" withString:@""];
data is the NSString with the line breaks. I'm pretty sure I implemented it correctly, just need to figure out what character I can use for a line break
 
Works for me.
Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	NSString* original = @"Hello \nThis is a line break \n And this is another one";

	NSString* result = [original stringByReplacingOccurrencesOfString:@"\n" withString:@""];

	NSLog( @"original: %@", original );
	NSLog( @"result: %@", result );
		
    [pool drain];
    return 0;
}

Command-line:
Code:
gcc replacing.m -framework Foundation && ./a.out

If the problem isn't apparent in the code you posted, maybe it lies in the code you didn't post. Try posting enough code for someone else to duplicate the problem.
 
ok, well heres what I have:

Code:
-(IBAction)logIn{
	// create the request
	NSString *url=[NSString stringWithFormat:@"http://www.dannyflax.comli.com/login.php?user=%@&pw=%@",username.text,password.text];
	NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	
	// create the connection with the request
	
	// and start loading the data
	
	NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	
	if (theConnection) {
		
		// Create the NSMutableData that will hold
		
		// the received data
		
		// receivedData is declared as a method instance elsewhere
		
		receivedData=[[NSMutableData data] retain];
		
	} else {
		UIAlertView *alertTest = [[UIAlertView alloc]
								  initWithTitle:@"Error"
								  message:@"Unable to connect. The server may be down or you may be unable to connect to the internet."
								  delegate:self
								  cancelButtonTitle:@"Ok"
								  otherButtonTitles:nil];
		
		
		[alertTest show];
		[alertTest autorelease];
		// inform the user that the download could not be made
		
	}
	
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{
	
    // this method is called when the server has determined that it
	
    // has enough information to create the NSURLResponse
	
	
	
    // it can be called multiple times, for example in the case of a
	
    // redirect, so each time we reset the data.
	
    // receivedData is declared as a method instance elsewhere
	
    [receivedData setLength:0];
	
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
	
    // append the new data to the receivedData
	
    // receivedData is declared as a method instance elsewhere
	
    [receivedData appendData:data];
	
}

- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error

{
	
    // release the connection, and the data object
	
    [connection release];
	
    // receivedData is declared as a method instance elsewhere
	
    [receivedData release];
	
	
	
    // inform the user
	
	UIAlertView *alertTest = [[UIAlertView alloc]
							  initWithTitle:@"Error"
							  message:@"Unable to connect. The server may be down or you may be unable to connect to the internet."
							  delegate:self
							  cancelButtonTitle:@"Ok"
							  otherButtonTitles:nil];
	
	
	[alertTest show];
	[alertTest autorelease];
	
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
	
    // do something with the data
	//"\n<script type=\"text/javascript\" src=\"http://analytics.hosting24.com/count.php\"></script>\n<noscript><a href=\"http://www.hosting24.com/\"><img src=\"http://analytics.hosting24.com/count.php\" alt=\"web hosting\" /></a></noscript><!-- End Of Analytics Code -->"
    // receivedData is declared as a method instance elsewhere
NSString *data=[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];	
	data=[data stringByReplacingOccurrencesOfString:@"<!-- www.000webhost.com Analytics Code -->" withString:@""];
	data=[data stringByReplacingOccurrencesOfString:@"<script type=\"text/javascript\" src=\"http://analytics.hosting24.com/count.php\"></script>" withString:@""];
	data=[data stringByReplacingOccurrencesOfString:@"<noscript><a href=\"http://www.hosting24.com/\"><img src=\"http://analytics.hosting24.com/count.php\" alt=\"web hosting\" /></a></noscript>" withString:@""];
	data=[data stringByReplacingOccurrencesOfString:@"<!-- End Of Analytics Code -->" withString:@""];
data=[data stringByReplacingOccurrencesOfString:@"\n" withString:@""];
	
	
	
	if([data isEqualToString:@"Success"]){
		UIAlertView *alertTest = [[UIAlertView alloc]
								  initWithTitle:@"Success!"
								  message:@"Successfully logged in!"
								  delegate:self
								  cancelButtonTitle:@"Ok"
								  otherButtonTitles:nil];
		
		
		[alertTest show];
		[alertTest autorelease];
		
	}
	else {
		UIAlertView *alertTest = [[UIAlertView alloc]
								  initWithTitle:@"Unable to log in"
								  message:data
								  delegate:self
								  cancelButtonTitle:@"Ok"
								  otherButtonTitles:nil];
		
		
		[alertTest show];
		[alertTest autorelease];
	}

    // release the connection, and the data object
	
    [connection release];
	
    [receivedData release];
	
}
As you can see, the program receives the data from my website, but my issue is that my host "hides" a tracking script in every page, which shows up when I try to receive data. What I'm trying to do is filter this script out of the result, but I'm having trouble removing the line breaks. I don't normally just post all of my code, but I guess this is the best way to get help.
 
You mean something like this?

Code:
NSString *str=[data stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Unfortunately that returns the same thing. By the way, thank you both for your help!
 
Aha! Found my answer:
Code:
data=[data stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
 
As you can see, the program receives the data from my website, but my issue is that my host "hides" a tracking script in every page, which shows up when I try to receive data. What I'm trying to do is filter this script out of the result, but I'm having trouble removing the line breaks.

First obvious question: How do you know the trouble lies in removing line breaks? I don't see anything obvious in the code that suggests trouble with line breaks. However, since you didn't post any output, how would anyone be able to tell?

What I do see is a very sensitive dependence on a very specific set of substrings, where even a single character's deviation will end up in no replacement being performed. If I were looking at things that might prevent recognition of "Success", that's where I'd start, not in the line break replacement.

Second obvious question: How do you know "\n" is the line-break? Many HTTP servers will use CRLF as a line-break.

EDIT:
And apparently, the server was using CRLF (or maybe unalloyed CR).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.