-3

我有一个NSString& 我想从中删除一些字符,当在其中找到第一个逗号时。

ex. str = @"0,1,2,3"; 
output - > str = @"1,2,3";

这该怎么做。谁能帮我解决这个问题。

4

4 回答 4

6

检查以下代码:

NSMutableArray *tempArray=[[NSMutableArray alloc]initWithArray:[yourString componentsSeparatedByString:@","]];
if([tempArray count] > 0)
{
   [tempArray removeObjectAtIndex:0];
}
NSString *output=[tempArray componentsJoinedByString:@","];
于 2012-12-19T15:02:18.927 回答
3
NSMutableArray *arr1 = [[str1 componentsSeparatedByString:@","] mutableCopy];
if ([arr1 count]){
    [arr1 removeObjectAtIndex:0];
}
NSString *finalStr = [arr1 componentsJoinedByString:@","];
于 2012-12-19T15:01:49.173 回答
3

一般情况:

NSString* str= @"0,1,2,3";
NSRange range= [str rangeOfString: @","];
if(range.location!=NSNotFound)
{
    str= [str substringWithRange: NSMakeRange(range.location+1, str.length-range.length-1)];
    NSLog(@"%@",str);
}

如果字符串没有逗号,它不会造成崩溃。

于 2012-12-19T15:04:57.843 回答
1

试试这个:

NSString *str1=@"0,1,2,3";
NSMutableArray *arr1=[[NSMutableArray alloc]initWithArray:[str1 componentsSeparatedByString:@","]];
if([arr1 count]>1)[arr1 removeObjectAtIndex:0];//as 1st comma is found. means atleast 2 elements must be there.
NSString *finalString=[arr1 componentsJoinedByString:@","];

注意:这个没有编译和检查..我只是在 Windows 中输入记事本。

于 2012-12-19T16:46:18.277 回答