我正在使用NSString类来显示一个格式为'STRING'的字符串,空格应显示在字符串的开头和结尾
我的代码是
NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
我也试过以下代码
NSString *STR_test = @" TEST ";
目前我的输出就像 '测试'
由于
答案 0 :(得分:2)
你可以通过多种方式做到这一点。
例如
// add the space in in front of string
NSString *finalString = [NSString stringWithFormat:@" %@",STR_test];
// calculate the string length and append the space in end of the string
NSString *result = [finalString stringByPaddingToLength:MAX(finalString.length + 1, STR_test.length)
withString:@" " startingAtIndex:0];
NSLog(@"final Result:\'%@\'", result);
另一个简单的选择
// add space in start and end of the string
NSString *finalString = [NSString stringWithFormat:@" %@ ",STR_test];
答案 1 :(得分:0)
嘿,我试着用你所做的,它在我的最后给出了完美的输出。检查附加的屏幕截图。
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_findmeetingtimes
如果您仍有问题,可以使用以下代码:
NSString *STR_test = @" TEST";
NSString *result = [NSString stringWithFormat:@"%@ ",STR_test];
NSLog(@"%@",result);
这就是我的尝试:
NSString * STR_test = @“TEST”; // NSLog(@“%@”,STR_test);
NSString *result = [NSString stringWithFormat:@"%@ ",STR_test];
NSLog(@"%@",result);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 50)];
label.text = [NSString stringWithFormat:@"%@",result];
label.backgroundColor = [UIColor redColor];
[label sizeToFit];
[self.view addSubview:label];
答案 2 :(得分:0)
我试过这个,它对我有用
NSString * reqString = [NSString stringWithFormat:@“%@%@%@”,@“”,@“YourString”,@“”]; 的NSLog(@ “%@”,reqString);
传递字符串代替字符串。
答案 3 :(得分:0)
<强>夫特强>
let str = "Hello"
var finalStr: String = " \(str)"
let result = finalStr.padding(toLength: max((finalStr.characters.count) + 1, (str.characters.count)), withPad: " ", startingAt: 0)
print("Result:\'\(result)\'")
答案 4 :(得分:0)
尝试如下:
NSString *MUT_STR = @" TEST";
MUT_STR = [MUT_STR stringByAppendingString:@" "];
NSLog(@"%@",MUT_STR);
答案 5 :(得分:0)
代码看起来很符合预期的结果。
案例1
NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;
With trailing and leading spaces
案例2
NSMutableString *MUT_STR = [NSMutableString stringWithString:@"TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;
案例3
NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
//[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;
案例4归因于文本
NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSBackgroundColorAttributeName];
NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:MUT_STR attributes:attributes];
//self.lbl.text = MUT_STR;
self.lbl.attributedText = attStr;
blue area is where there is text content
确保您没有修剪尾随空格或不在代码流中的任何位置替换空格