Look at using the NSRegularExpression
class to find each substring you need to operate on.
Objective-C: Find and Replace in String
-
28-06-2022 - |
Pregunta
I have a string, dynamically generated from a website, that I would like to change.
I already know you need to change the string to a NSMutableString
, but the part I want to remove is almost never the same. (e.g. [XXXXX]
or [XABXX]
... there are thousands of combos).
The only static parts are the square brackets on either end and that there are 5 characters in between.
How would I recognise and remove this string of seven characters ([*****]
, with the *'s representing a random letter)?
Solución 2
Otros consejos
A simple search and you can find it here:
with a simple change:
- (NSString *)stringCleaner:(NSString *)yourString {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:yourString];
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:@"[" intoString:NULL] ;
[theScanner scanUpToString:@"]" intoString:&text] ;
yourString = [yourString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@]", text] withString:@""];
}
return yourString;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow