gpt4 book ai didi

iphone - 为什么Xcode在字符串末尾添加空格?

转载 作者:行者123 更新时间:2023-12-01 17:42:13 25 4
gpt4 key购买 nike

我是iPhone开发的新手,而xcode的问题很奇怪。由于某种原因,它会在字符串的末尾随机添加尾随空格。我的初始字符串值是从xml中解析的,然后从那里尝试获取字符串的最后一部分。由于这种随机空白,事实证明这很困难。任何帮助是极大的赞赏。

我的密码

//initial String 
<title>March 15 2013 Metals Commentary: Thomas Vitiello</title>

//Parsing
NSString *name = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSLog(@"myname: %@ %i", name, name.length);

//Log at this point
myname: March 15 PM Metals Commentary: Thomas Vitiello
59

//Attempt at Removing Whitespace
NSArray *lastName=[name componentsSeparatedByString:@" "];
name = [[lastName objectAtIndex:6]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"myname: %@ %i", name, name.length);

//Log at this point
myname: Vitiello
9 //<--Should be 8, not 9. This annoying whitespace is also particularly long, taking more than 1 character space, despite obviously being 1 character long.

最佳答案

您也可以尝试从字符串中删除换行符。您的字符串不仅包含空格,而且还包含换行符。

NSString *trimmmedContent = [contents stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

关于iphone - 为什么Xcode在字符串末尾添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15464320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com