/images/content/booking_thumbs_uk/s_kl/50000/THB_999_H54007.jpg
changes to:
/images/content/booking_thumbs_uk/s_kl/00000/THB_999_H2470.jpg
NSString* original=#"\\/images\\/content\\/booking_thumbs_uk\\/s_kl\\/50000\\/THB_999_H54007.jpg";
NSString* removed=[original stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSLog(#"%#",removed); // shows /images/content/booking_thumbs_uk/s_kl/00000/THB_999_H2470.jpg
Be very careful, because inside the source code between "..." the backslash has a special meaning. In order to represent an honest backslash, you need to double it, like "\\".
You can use newString = [oldString stringByReplacingOccurrencesOfString:#"\\" withString:#""];
Related
I am trying to remove the front character of a NSMutableString and have tried:
[str setString: [str substringFromIndex:1]] ,
[str deleteCharactersInRange:NSMakeRange(0,1)]
and a few others. These two either remove every character or every character except for the one I am trying to remove so for example if I have the String "-2357" and am trying to remove the "-" then I get just the "-" when I should get "2357".
Am I doing something wrong, and how can I fix this?
If you want to use the deleteCharactersInRange method, do this
NSRange range = {0,1};
[str deleteCharactersInRange:range];
Or, you could use substringFromIndex like so:
str = [str substringFromIndex:1];
Contrary to what you are saying, [str deleteCharactersInRange:NSMakeRange(0,1)] should work fine, I don't know why that is not working.
[str substringToIndex:1] will return the string from the beginning to the index 1, which is -. What you want is substringFromIndex. And as this method returns a NSString, you will need to assign it to str; just calling the method on str is not enough.
Edit
Using the deleteCharactersInRange may pose problems for some type of characters. For more information refer to this a question here.
Instead, you can safely use
[str deleteCharactersInRange:[str rangeOfComposedCharacterSequenceAtIndex:0]]
instead of:
[str substringToIndex:1]
Do this:
[str substringFromIndex:1]
If you want to remove first character of the string you can use this:-
string = [string substringFromIndex:1];
I would like to know how it is possible to omit a specific substring out an NSString, assuming the NSString does contain that substring.
For example:
Original string: "This is a string but these words should be omitted."
Substring to omit: "but these words should be omitted".
Result string: "This is a string."
Thanks ahead,
iLyrical.
NSString *originalString = #"This is a string but these words should be omitted.";
NSString *substringToOmit = #" but these words should be omitted";
NSString *resultString = [originalString stringByReplacingOccurencesOfString:substringToOmit
withString:#""];
See NSString's stringByReplacingOccurrencesOfString:withString:. You may also want to trim the trailing whitespace with stringByTrimmingCharactersInSet:.
Hoping somebody can help me out - I would like to replace a certain character in a string and am wondering what is the best way to do this?
I know the location of the character, so for example, if I want to change the 3rd character in a string from A to B - how would I code that?
If it is always the same character you can use:
stringByReplacingOccurrencesOfString:withString:
If it is the same string in the same location you can use:
stringByReplacingOccurrencesOfString:withString:options:range:
If is just a specific location you can use:
stringByReplacingCharactersInRange:withString:
Documentation here:
https://developer.apple.com/documentation/foundation/nsstring
So for example:
NSString *someText = #"Goat";
NSRange range = NSMakeRange(0,1);
NSString *newText = [someText stringByReplacingCharactersInRange:range withString:#"B"];
newText would equal "Boat"
NSString *str = #"123*abc";
str = [str stringByReplacingOccurrencesOfString:#"*" withString:#""];
//str now 123abc
Here is the code:
[aString stringByReplacingCharactersInRange:NSMakeRange(3,1) withString:#"B"];
Use the replaceCharactersInRange: withString: message on a NSMutableString object.
hey all i want a code the replace whitespaces by a + sign in objective-c
In case you are asking this because you need to encode URLs, use this
NSString* escapedUrlString =
[unescapedString stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
If you just need space to +, use
[str stringByReplacingOccurrencesOfString:#" " withString:#"+"];
return [thatString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
If your real target is to escape URL component, use the -stringByAddingPercentEscapesUsingEncoding: method instead.
I have a string like this "A. rahul VyAs"
and i want to remove "A. " and the space after the "A." so that new string would be "rahul VyAs"
How do i achieve this?
You can use the NSString instance methods substringWithRange: or substringFromIndex:
NSString *str = #"A. rahul VyAs";
NSString *newStr = [str substringWithRange:NSMakeRange(3, [str length]-3)];
or
NSString *str = #"A. rahul VyAs";
NSString *newStr = [str substringFromIndex:3];
This is a solution I have seen specifically for removing regularly occurring prefixes and solving the answer to the question How do I remove "A. "?
NSString * name = #"A. rahul VyAs";
NSString * prefixToRemove = #"A. ";
name = [name stringByReplacingOccurrencesOfString:prefixToRemove withString:#""];
This code will remove what you tell it to remove/change if the character set exists, such as "A. ", even if the three characters (or more/less) are in the middle of the string.
If you wanted to remove rahul, you can. It's diverse in that you specify exactly what you want removed or changed, and if it exists anywhere in the String, it will be removed or changed.
If you only want a certain specified number of characters removed from the front of the text that are always random or unknown, use the [string length] method as is the top answer.
If you want to remove or change certain characters that repeatedly appear, the method I have used will enable that, similar to Wordsearch on document editors.
Try this,
char *string=[#"A. rahul VyAs" cStringUsingEncoding:NSUTF8StringEncoding];
char *subString=&name[3];
NSString *newString=[NSString stringWithCString:subString encoding:NSUTF8StringEncoding];
It's this simple:
myString = [myString subStringFromIndex:3]
That's it.