0

I have a serious problem about indexing in array. I've been working on this for 2 days and couldn't find answer yet.

I want to do that, search specific character in array then replace it with other string. I'm using replaceObjectAtIndex method but my code is doesn't work.

Here is my code;

    NSString *commentText = commentTextView.text;
    NSUInteger textLength = [commentText length];
    NSString *atSign = @"@";
    NSMutableArray *commentArray = [[NSMutableArray alloc] init];
    [commentArray addObject:commentText];

    for (int arrayCounter=1; arrayCounter<=textLength; arrayCounter++)
    {
        NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch];
        if(isRange.location != NSNotFound)
        {
            commentText = [commentText stringByReplacingOccurrencesOfString:commentText withString:atSign];
            [_mentionsearch filtrele:_mentionText];
            id<textSearchProtocol> delegate;
            [delegate closeList:[[self.searchResult valueForKey:@"user_name"] objectAtIndex:indexPath.row]];
        }
    }

Ok, now i can find "@" sign in the text and i can match it. But this is the source of problem that, i can not replace any string with "@" sign. Here is the last part of code;

    -(void)closeList
    {
        NSArray *arrayWithSign = [commentTextView.text componentsSeparatedByString:@" "];
        NSMutableArray *arrayCopy = [arrayWithSign mutableCopy];
        [arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:@"@%@",username]];
    }

When im logging isRange.location value, it returns correct. But when im try to run, my application is crashing. So, i can not replacing [NSString stringWithFormat:@"@%@",username] parameter. How can i solve this problem?

6
  • 1
    How is it crashing? You have to include the crash log. Commented Jan 24, 2014 at 12:18
  • Why are you trying to use a range taken from a string search as the index into an array? Commented Jan 24, 2014 at 12:22
  • There is no log downside. Only thing i have, (lldb) 0x6ae8952:jae 0x6ae8962 ; __pthread_kill + 26. Commented Jan 24, 2014 at 12:22
  • In NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch]; and in [arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:@"@%@",username]]; variable isRange is same? Commented Jan 24, 2014 at 12:44
  • 1
    Try this to remove @ from string NSString *replacedstr=[YourString stringByReplacingOccurrencesOfString:@"@" withString:@""]; Commented Jan 24, 2014 at 12:52

1 Answer 1

2

If I understand correctly you want to change a substring in a string with a new string. In this case, why don't you use directly the stringByReplacingOccurrencesOfString method of NSString:

NSString *stringToBeChanged = @"...";
NSString *stringToBeChangedWith = @"...";
NSString *commentTextNew = [commentText stringByReplacingOccurrencesOfString:stringToBeChanged withString:stringToBeChangedWith];
Sign up to request clarification or add additional context in comments.

2 Comments

I did big mistake about array results. That is solution thx :)
i want to change the string in between "<>" <my stirng> to < new tring> my string is they dynamic string then how can i identify the string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.