如何有选择地修剪 NSMutableString?

发布于 2024-12-03 15:19:11 字数 321 浏览 7 评论 0原文

我想知道如何有选择地修剪 NSMutableString。例如,如果我的字符串是“MobileSafari_2011-09-10-155814_Jareds-iPhone.plist”,我将如何以编程方式删除除“MobileSafari”一词之外的所有内容?

注意:考虑到上面的术语以编程方式,我希望即使将“MobileSafari”一词更改为“Youtube”或“Jared's-”一词,该解决方案也能发挥作用。 iPhone”更改为“Angela's-iPhone”。

非常感谢任何帮助!

I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?

Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".

Any help is very much appreciated!

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

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

发布评论

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

评论(5

阪姬 2024-12-10 15:19:11

鉴于您始终需要提取第一个下划线之前的字符;使用以下方法;

NSArray *stringParts = [yourString componentsSeparatedByString:@"_"];

我认为数组中的第一个对象将是您需要的提取部分。

Given that you always need to extract the character upto the first underscore; use the following method;

NSArray *stringParts = [yourString componentsSeparatedByString:@"_"];

The first object in the array would be the extracted part you need I would think.

反话 2024-12-10 15:19:11

测试代码:100% 有效

NSString *inputString=@"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";

NSArray *array= [inputString componentsSeparatedByString:@"_"];

if ([array count]>0) {

    NSString *resultedString=[array objectAtIndex:0];


    NSLog(@" resultedString IS - %@",resultedString);



}

输出:

resultedString IS - MobileSafari

TESTED CODE: 100% WORKS

NSString *inputString=@"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";

NSArray *array= [inputString componentsSeparatedByString:@"_"];

if ([array count]>0) {

    NSString *resultedString=[array objectAtIndex:0];


    NSLog(@" resultedString IS - %@",resultedString);



}

OUTPUT:

resultedString IS - MobileSafari
痴情换悲伤 2024-12-10 15:19:11

如果您知道字符串的格式总是这样,那就很容易了。

只需使用 NSString 的 componentsSeparatedByString: 记录的 此处

在您的情况下,您可以这样做:

NSString *source = @"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";

NSArray *seperatedSubStrings = [source componentsSeparatedByString:@"_"];

NSString *result = [seperatedSubStrings objectAtIndex:0];

@"MobileSafari" 位于索引 0,@"2011-09-10-155814" 位于索引 1,@“Jareds-iPhone.plist” 并位于索引 2 处。

If you know the format of the string is always like that, it can be easy.

Just use NSString's componentsSeparatedByString: documented here.

In your case you could do this:

NSString *source = @"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";

NSArray *seperatedSubStrings = [source componentsSeparatedByString:@"_"];

NSString *result = [seperatedSubStrings objectAtIndex:0];

@"MobileSafari" would be at index 0, @"2011-09-10-155814" at index 1, and @"Jareds-iPhone.plist" and at index 2.

怀里藏娇 2024-12-10 15:19:11

试试这个:

NSString *strComplete = @"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";  
NSArray *arr = [strComplete componentsSeparatedByString:@"_"];  
NSString *str1 = [arr objectAtIndex:0];  
NSString *str2 = [arr objectAtIndex:1];  
NSString *str3 = [arr objectAtIndex:2]; 

str1 是必需的字符串。
即使您将 MobileSafari 更改为 youtube,它仍然可以工作。

Try this :

NSString *strComplete = @"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";  
NSArray *arr = [strComplete componentsSeparatedByString:@"_"];  
NSString *str1 = [arr objectAtIndex:0];  
NSString *str2 = [arr objectAtIndex:1];  
NSString *str3 = [arr objectAtIndex:2]; 

str1 is the required string.
Even if you change MobileSafari to youtube it will work.

时光暖心i 2024-12-10 15:19:11

因此,您需要一个 NSString 变量来保存要截断的字符串的开头。之后,一种方法可能是同时更改字符串和变量字符串值。假设变量字符串是“Youtube”,而不是更改为“MobileSafari”,那么可变字符串应该从“MobileSafari_.....”更改为“YouTube_......”。然后您可以获得可变字符串长度并使用以下代码来截断可变字符串。

NSString *beginningOfTheStr;
.....
theMutableStr=[theMutableStr substringToIndex:[beginningOfTheStrlength-1]]; 

看看这是否适合你。

So you'll need an NSString variable that'll hold the beginning of the string you want to truncate. After that one way could be to change the string and the variable string values at the simultanously. Say, teh Variable string was "Youtube" not it is changed to "MobileSafari" then the mutable string string should change from "MobileSafari_....." to "YouTube_......". And then you can get the variable strings length and used the following code to truncate the the mutable string.

NSString *beginningOfTheStr;
.....
theMutableStr=[theMutableStr substringToIndex:[beginningOfTheStrlength-1]]; 

See if tis works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文