如何从 NSString中仅删除开头和结尾的新行\n 。
例如
@"\n\n\n\Hi\nHow are you\nToday.\n\n\n\n"
输出:
@"Hi\nHow are you\nToday."
谢谢。
如何从 NSString中仅删除开头和结尾的新行\n 。
例如
@"\n\n\n\Hi\nHow are you\nToday.\n\n\n\n"
输出:
@"Hi\nHow are you\nToday."
谢谢。
使用nsstring的 stringByTrimmingCharactersInSet 方法,该方法返回一个新字符串,该字符串是通过从给定字符集中包含的接收器字符的两端删除而生成的。
yourString = yourString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
yourString = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]
NSString *string = @"\n \n new lines or white spaces in front and at the end \n \n ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
斯威夫特 3.0
var yourString = "\n your string \n"
yourString = yourString.trimmingCharacters(in: .whitespacesAndNewlines)
输出 "your string"
试试这个它对我有用,
NSString *trimmedValue = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
希望这会有所帮助。