0

i have a sting in which i want to replace all characters(except special characters) by X.. i mean if i have a string str= mac,iphone & ipad are products of apple

theis should be converted to

str= XXX,XXXXXX & XXXX XXX XXXXXXXX XX XXXXX

i know this can be done by finding all special characters and note their position and then replace all other characters except these special characters but there are so many special characters should i check them one by one ? or is there any other method to identify them

please help

5 Answers 5

5

NSString *str=@"mac,iphone & ipad";

for(int i=0;i<[str length];i++)
{
    int str1=(int)[str characterAtIndex:i];
    NSString *temp=[NSString stringWithFormat:@"%C",str1];

    if(str1 >96 && str1 <123  || str1 >64 && str1 <91)

        str = [str stringByReplacingOccurrencesOfString:temp withString:@"X"];
}

Hope this helps...This will replace all characters either in upper/lower case

Sign up to request clarification or add additional context in comments.

1 Comment

good work sherry....it worked and i become to know some special....moves....voted and accepted will you please answer my another question here stackoverflow.com/questions/3869790/…
2
NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); 

Try this. It is more useful when the above answered ways don't replace the characters.

1 Comment

interworld and your org name whats that?
1
[Nsstring stringByReplacingOccurrencesOfString:@"" withString:@"X"];

from this you can replace string according to my knowledge

3 Comments

it does not works good for my problem this is not for all characters from a-z
just take one array and store all in array and then compare each array object if it is alphabet then change it into X other wise save as it in append string
actually the main problem is ..how to check that current character is alphabet
1
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(TextFound)
    {
        NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/<br />"];
        string = [[string componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

        TextStr = [TextStr stringByAppendingString:string];
    }
}

As I did it in my parsing, it was showing linebreak character in the text. I tried the way stringbyrplacingoccurance of string, but nothing happened, so finally it worked.

Comments

0

I'd use a regular expression. Check out the documentation for NSRegularExpression

Comments

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.