gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:16:55 28 4
gpt4 key购买 nike

当尝试使用 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/

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