iphone - NSMutableString appendFormat ... 添加空格?

标签 iphone ios nsstring

当尝试使用 appendFormat 附加到 NSMutableString 时 - 它会添加空格。

NSM 只是一个 NSMutableString,att_1_variable 和 att_2_variable 是 NSString

[NSM appendFormat:@"<tagname att_1=\" %@ \" att_2=\" %@ \">", att_1_variable, att_2_variable];

结果是:

<tagname myattribute="  ContentOfVariable  " title=" ContentOfVariable  ">

在传入我正在做的字符串之前:

    NSString* att_1_variable = [att_1_variable_orginal stringByTrimmingCharactersInSet:
                  [NSCharacterSet whitespaceAndNewlineCharacterSet]];

有什么办法解决这个问题吗?

谢谢 问候 基督徒

最佳答案

您要自己添加空格,方法是将它们包含在格式字符串中。在 C 中,引号的转义序列只是 \",没有尾随(或前导)空格。所以你想要:

[NSM appendFormat:@"<tagname myattribute=\"%@\" title=\"%@\">", 
                                 attributeVariable, titleVariable];

如果引号和之后的变量内容之间有空格,那么您的输入变量将用空格填充。你可以用类似的东西修剪那些:

NSString *trimmedAttributeVariable = [attributeVariable
        stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

...

[NSM appendFormat:@"<tagname myattribute=\"%@\" title=\"%@\">", 
                                 trimmedAttributeVariable, ...

这将从两端修剪空格和制表符。

关于iphone - NSMutableString appendFormat ... 添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057678/

相关文章:

ios - 为什么 Array 在 Swift 中是 AnyObject?

ios - 如何在 Swift 的表格 View 单元格中进行有限的多重复选标记?

iphone - 使用 unicode 的分数的 NSString 表示

objective-c - NSArray 和 NSString

iphone - Objective-C - 将 GPS 信息添加到照片

iphone - UIWebView 编辑 HTML 内容

ios - 如何在 Swift 中检查 SQLite 表是否为空

ios - 长 UIButton 标题标签在中心而不是在右侧截断

ios - 某些代码未在从另一个 ViewController 调用的方法中执行

iphone - NSUserDefaults 无法正常工作