I am aware of replacing Strings of a String, but that only works if I know exactly what I want to remove.
If I have a String like the following:
"hi-there-this-is-a-test&feature=hi-there"
How do I remove '&feature' and everything that comes after that?
Any help would be greatly appreciated. Thanks in advance!
EDIT: If absolutely necessary to use REGEX, could someone show me how to use it? I am aware it is 10.7 onwards but I'm fine with that. Even better, an example of String trimming or using the NSScanner?
Thanks again everyone.
EDIT: The solution posted below is the correct one, but resulted in a crash for me. This is how I solved the problem:
NSString *newString = [[oldString componentsSeparatedByString: @"&feature="] objectAtIndex:0];
Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .
To remove everything after the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part before the separator to the original string variable.
Solution 1. string str = "this is a #string"; string ext = str. Substring(0, str. LastIndexOf("#") + 1);
It can be done without REGEX like this:
NSString *string = @"hi-there-this-is-a-test&feature=hi-there";
NSRange range = [string rangeOfString:@"&feature"];
NSString *shortString = [string substringToIndex:range.location];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With