【问题标题】:How to add more strings to the combined string if the length is less than prescribed length?如果长度小于规定长度,如何向组合字符串添加更多字符串?
【发布时间】:2021-02-16 19:37:36
【问题描述】:

在 iOS 中,我使用两个字符串来组合并形成一个字符串。但组合字符串必须为 16 个字符。因此,如果两个字符串都很小,并且如果我们将两者结合起来并且少于 16 个字符,我必须添加更多字符以使其达到 16 个字符。如何做到这一点?

NSString *combined = [NSString stringWithFormat:@"%@%@", stringURL, stringSearch];

这是我正在使用的代码。那么如果我组合起来少于 16 个字符如何计算它并添加更多字符使其成为 16 个字符?

【问题讨论】:

    标签: ios objective-c iphone xcode nsstring


    【解决方案1】:

    类似下面的东西

    NSString *combined = [NSString stringWithFormat:@"%@%@", stringURL, stringSearch];
    if (combined.length < 16)
    {
        NSString *newCombined = [NSString stringWithFormat:@"%@%@", combined, @"Some new string"];
    }
    

    您可以使用substringWithRange: 来自NSString 的方法。您可以以下面的代码为例,根据您的需要进行修改。

    if (combined.length > 25)
    {
        NSString *beginning = [combined substringWithRange:NSMakeRange(0, 15)];
        NSString *fromEnd = [combined substringWithRange:NSMakeRange(startPoint, combined.length-startPoint)];
    }
    

    【讨论】:

    • Maulik 谢谢...如果可行,我会尝试并接受它。但是如果字符串的长度超过 25 个字符,你能帮我删除字符吗?例如,如果我有一个 40 个字符的字符串,我必须删除 15 个字符(从开头或从结尾)。如何做到这一点?
    • @Fionashoff 我已经添加了一些示例代码,所以至少您可以从它开始并根据您的要求进行修改。
    • 您能告诉我有关端点和起点的信息吗? :)
    • 它是您希望字符串开始和结束的索引号。
    • 你能举个例子吗?
    【解决方案2】:

    如果你只想用一个字符填充,你可以使用stringWithFormat - 基本上是printf。下面我给出一些我构建的例子来说明,所以它不会用完框,但你只需要注释掉你不想让它工作的那些。

            // To get 50 spaces
            NSString * s50 = [NSString stringWithFormat:@"%*s", 50, ""];
    
            // Pad with these characters, select only 1
    
            // This will pad with spaces
            char * pad = "";
    
            // This will pad with minuses - you need enough to fill the whole field
            char * pad = "-------------------------------------------------------";
    
            // Some string
            NSString * s = @"Hi there";
    
            // Here back and front are just int's. They must be, but they can be calculated,
    
            // e.g. you could have this to pad to 50
            int back = 50 - s.length; if ( back < 0 ) back = 0;
    
            // Pad s at the back
            int back = 20;
            NSString * sBack = [NSString stringWithFormat:@"%@%*s", s, back, pad];
    
            // Pad s in front
            int front = 10;
            NSString * sFront = [NSString stringWithFormat:@"%*s%@", front, pad, s];
    
            // Pad s both sides
            NSString * sBoth = [NSString stringWithFormat:@"%*s%@%*s", front, pad, s, back, pad];
    

    请注意,此处的数量是参数化的。我使用例如50 在第一行,但也可以是 n,只要 n 是一个 int 并且您可以使用它来执行计算,将其存储在 n 和填充中。代码中有一个例子。

    这是一个输出示例

    2020-11-04 08:16:22.908828+0200 FormatSpecifiers[768:15293] [Hi there-------------------------------------------------------]
    2020-11-04 08:16:22.908931+0200 FormatSpecifiers[768:15293] [-------------------------------------------------------Hi there]
    2020-11-04 08:16:22.908992+0200 FormatSpecifiers[768:15293] [-------------------------------------------------------Hi there-------------------------------------------------------]
    

    我只是展示如何填充组合字符串。要组合字符串当然只需使用stringByAppendingString 例如

    NSString * s = [a stringByAppendingString:b];
    

    然后您可以根据s.length 进行计算,例如如示例所示。

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2017-06-27
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多