Removing new line characters from NSString
Split the string into components and join them by space:
NSString *newString = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
Remove beginning and trailing new line characters from NSString
Use stringByTrimmingCharactersInSet method of nsstring which Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
Swift
yourString = yourString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
Objective-C
yourString = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]
How do I remove new line characters from a string?
stringByReplacingOccurrencesOfString
does not modify textLine
NSString *strippedTextLine = [textLine stringByReplacingOccurrencesOfString:@"\n" withString:@""];
Or
NSString *textLine = @"My cool text\n";
textLine = [textLine stringByReplacingOccurrencesOfString:@"\n" withString:@""];
Remove leading and trailing newline characters from NSString
text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
This trims also whitespace.
If you want to keep whitespace, just use the newlineCharacterSet
instead.
Edited, as per @ghettopia correct suggestion about using the newlineCharacterSet instead of creating. Custom one.
NSString replace repeated newlines with single newline
You might use NSRegularExpression. This is the most simple and elegant way:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\n+" options:0 error:NULL];
NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"\n"];
Remove newline character from first line of NSString
This should do the trick:
NSString * ReplaceFirstNewLine(NSString * original)
{
NSMutableString * newString = [NSMutableString stringWithString:original];
NSRange foundRange = [original rangeOfString:@"\n"];
if (foundRange.location != NSNotFound)
{
[newString replaceCharactersInRange:foundRange
withString:@""];
}
return [[newString retain] autorelease];
}
Related Topics
Convert an Nsurl to an Nsstring
How to Remove Border of the Navigationbar in Swift
iOS Nested View Controllers View Inside Uiviewcontroller's View
Xcode 10, Command Codesign Failed with a Nonzero Exit Code
Error: Cuicatalog: Invalid Asset Name Supplied: (Null), or Invalid Scale Factor:2.000000
Creating Thumbnail from Local Video in Swift
Xcode Debugger Doesn't Print Objects and Shows Nil, When They Aren'T
iOS 7 Uisearchdisplaycontroller Search Bar Overlaps Status Bar While Searching
Avplayer Stops Playing and Doesn't Resume Again
Uirefreshcontrol on Uicollectionview Only Works If the Collection Fills the Height of the Container
iOS Uialertview Button to Go to Setting App
iOS 11 Safe Area Layout Guide Backwards Compatibility
In Swiftui, How to Use Uihostingcontroller Inside an Uiview or as an Uiview
iOS Storyboard Passing Data Navigationviewcontroller
Swift - How to Record Video in Mp4 Format with Uiimagepickercontroller
Changed +Load Method Order in Xcode 7
Navigation Pop View When Swipe Right Like Instagram iPhone App.How I Achieve This