【问题标题】:iOS: Create String from Variable and string combinediOS:从变量和字符串组合创建字符串
【发布时间】:2014-04-14 16:33:41
【问题描述】:

我需要将一个字符串和一个变量连接在一起 - 我找到了很多在变量之前添加字符串的示例,但反之则不然 - 我该怎么做?

 NSString *theImage = [[self.detailItem valueForKey:@" resimagefiletitle"] description], @"'add on the end";

【问题讨论】:

  • 我想这就是你要找的。 stackoverflow.com/questions/384089/…
  • 欢呼@ungot Basa - 基本上我需要这个相反的,即变量然后字符串的串联 - 不是字符串然后变量 - 这是poss吗?
  • 当然可以。只需使用 [NSString stringWithFormat:@"%@ string I want added on end", variableName]。这假设 variableName 返回一个 NSString。

标签: ios objective-c


【解决方案1】:

类似这样的:

NSString *theImage = [NSString stringWithFormat:@"%@ %@",[self.detailItem valueForKey:@" resimagefiletitle"], @"'add on the end"];

或者:

NSString *theImage = [NSString stringWithFormat:@"%@ add on the end",[self.detailItem valueForKey:@" resimagefiletitle"]];

【讨论】:

    【解决方案2】:

    试试这个

    NSString *theImage = [NSString stringWithFormat:@"%@ '>",[[self.detailItem valueForKey:@"resimagefiletitle"] description]];

    这里我在考虑[[self.detailItem valueForKey:@"resimagefiletitle"] description]给NSString

    【讨论】:

      【解决方案3】:

      我们可以通过提及它的格式将不同类型的数据类型连接成字符串。

      如果您想将两个或多个字符串连接在一起,那么您可以使用以下代码:

      NSString *NewString = [NSString stringWithFormat:@"%@%@",@"This Is",@"连接字符串的方式"];

      如果你想要连接整数值,那么你可以提到它的数据格式“%i”。 例如: int OutOf = 150; NSString *NewString = [NSString stringWithFormat:@"%@%i",@"I got 100 out of ",OutOf];

      这可能会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-14
        • 2011-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多