如何有选择地修剪 NSMutableString?
我想知道如何有选择地修剪 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
鉴于您始终需要提取第一个下划线之前的字符;使用以下方法;
我认为数组中的第一个对象将是您需要的提取部分。
Given that you always need to extract the character upto the first underscore; use the following method;
The first object in the array would be the extracted part you need I would think.
测试代码:100% 有效
输出:
TESTED CODE: 100% WORKS
OUTPUT:
如果您知道字符串的格式总是这样,那就很容易了。
只需使用 NSString 的
componentsSeparatedByString:
记录的 此处。在您的情况下,您可以这样做:
@"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:
@"MobileSafari"
would be at index 0,@"2011-09-10-155814"
at index 1, and@"Jareds-iPhone.plist"
and at index 2.试试这个:
str1 是必需的字符串。
即使您将 MobileSafari 更改为 youtube,它仍然可以工作。
Try this :
str1 is the required string.
Even if you change MobileSafari to youtube it will work.
因此,您需要一个 NSString 变量来保存要截断的字符串的开头。之后,一种方法可能是同时更改字符串和变量字符串值。假设变量字符串是“Youtube”,而不是更改为“MobileSafari”,那么可变字符串应该从“MobileSafari_.....”更改为“YouTube_......”。然后您可以获得可变字符串长度并使用以下代码来截断可变字符串。
看看这是否适合你。
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.
See if tis works for you.