交换字符串中的两个数字

发布于 2025-01-07 23:55:28 字数 142 浏览 4 评论 0原文

我有一个包含两个数字和一些文本的字符串。我想交换这两个数字,例如。 “22-33”在第二个字符串中将是“33-22”。这可能吗?

该字符串将包含不同的数字,但每个数字仅包含两个数字,并且它们将保留在字符串中的相同位置。

有谁可以帮助我吗?

I have a string that contains two numbers and some text. I want to swap the two numbers with each other, ex. "22-33" will be "33-22" in a second string. Is that possible?

The string will be with different numbers, but only with two digts each and they will stay at the same place in the string.

Anyone that can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2025-01-14 23:55:29

当然,首先像这样分割它们:

NSString *myString = @"22-33";
    NSArray *strings = [myString componentsSeparatedByString:@"-"];
    NSString  *reversedNumbers = [[[strings lastObject] stringByAppendingString:@"-"] stringByAppendingString:[strings objectAtIndex:0]];

Sure just split them first like this:

NSString *myString = @"22-33";
    NSArray *strings = [myString componentsSeparatedByString:@"-"];
    NSString  *reversedNumbers = [[[strings lastObject] stringByAppendingString:@"-"] stringByAppendingString:[strings objectAtIndex:0]];
绝不放开 2025-01-14 23:55:29
NSArray *components = [string componentsSeparatedByString:@"-"];
NSString *newString = [NSString stringWithFormat:@"%@-%@", [components objectAtIndex:1], [components objectAtIndex:0]];
NSArray *components = [string componentsSeparatedByString:@"-"];
NSString *newString = [NSString stringWithFormat:@"%@-%@", [components objectAtIndex:1], [components objectAtIndex:0]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文