Replace occurrences of NSString with `'` to `\'` - ios

I have a NSString in which I m trying to replace special characters. Part of my string looks like this:
I have replace ' to \'

If you want to replace ' with \' use
NSString *str = #"ladies'";
str = [str stringByReplacingOccurrencesOfString:#"'" withString:#"\\'"];
\ is an escape character so you'll have to use it twice.

Note: \ is escape character in objective c.
NSString *s = #"This is Testing Mode and we are testing just Details of this Ladies' Market place";
NSString *r = [s stringByReplacingOccurrencesOfString:#"'" withString:#"\\'"];

NSString *str = #"'Hello'";
str = [str stringByReplacingOccurrencesOfString:#"'" withString:#"\\"];

NSString *originalString = "a rand'om st'ring"
NSString *resultString = [originalString stringByReplacingOccurrencesOfString:#"'" withString:#"\'"];
// "a random string" (output of "po resultString" in LLDB)
using the escape character "\", we can remove unwanted characters. The character next to escape character("\") will be ignored/escaped.

Related

How do I remove new line characters from a string?

I need to strip away any new line characters from a string. I tried the stringByReplacingOccurencesOfString method below. What is the correct method to do this in Objective C?
[textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *textWithNewLinesRemoved = [textLine stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
stringByReplacingOccurrencesOfString does not modify textLine
NSString *strippedTextLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];
Or
NSString *textLine = #"My cool text\n";
textLine = [textLine stringByReplacingOccurrencesOfString:#"\n" withString:#""];

string modifications are not working?

This is my code and in my url string white spaces are not encoded by the code
NSString *str = item.fileArtPath;
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
[str stringByAddingPercentEncodingWithAllowedCharacters:set];
[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *urlString = [NSURL URLWithString:str];
for below string:
http://xx.xx.xx.xxx:xx/xx/xx/xx/xxx/Audio Adrenaline.jpg
^^^^^^^^^^^^^^^^^^^^
The white space after Audio is not converted to %20 after I used string replacement. And in Debugging urlString is nil why so?
From the NSString Class Reference
Returns a new string made from the receiver by replacing all
characters not in the specified set with percent encoded characters.
Meaning it doesn't permute the instance it's called on. You need to say something like str = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; Same with [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Recall that NSString is immutable. Calling methods stringBy... returns the modified string, rather than modifying the original one. Therefore your code should be rewritten as follows:
str = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *search = [searchBar.text stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

objective-c regex to remove NSString characters if they start with spaces and commas

My app uses addresses. I am not sure how to create a regex that strips away an address that reads like these.
" , Paxton, TX"
" , Dallas TX"
Need the above to be stripped so they read like this.
"Paxton, TX"
"Dallas TX"
Use the NSString API stringByTrimmingCharactersInSet:
NSString *test = #" , Paxton, TX";
NSMutableCharacterSet *charset = [[NSCharacterSet whitespaceCharacterSet] mutableCopy];
[charset addCharactersInString:#","];
NSString *final = [test stringByTrimmingCharactersInSet:charset];
NSLog(#"final = [%#]",final);
You can also do this without using regular expressions
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#" ,"];
NSString *trimmed = [#" , Payton, TX" stringByTrimmingCharactersInSet:charSet];
NSLog(#"%#", trimmed); //prints "Payton, TX"

How to remove charactor '\' in NSString?

How to remove character '\' in NSString IOS
For example,
NSString *abc = #"bmcvn\nmsf;
I have tried:
NSString *stri = #"\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:#"\\" withString:#"123"];
NSLog(str);
but it didn't replace
Note the difference between replacing the backslash char
NSString *original = #"foo\\bar";
NSLog(#"%#", original); // Prints: foo\bar
NSString *replaced = [original stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSLog(#"%#", replaced); // Prints: foobar
And replacing a char that is represented using a backslash (\r, \n, \t, ...):
NSString *original = #"thisIsCarriageReturn\rRightThere";
NSLog(#"%#", original); // Prints: thisIsCarriageReturn
// RightThere
NSString *replaced = [original stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSLog(#"%#", replaced); // Prints: thisIsCarriageReturnRightThere
You can't replace "\" in the string "\rdffsdf" because it is part the two character representation "\r" of the single character with the hex value 0x0d (13 decimal). It is the "carriage return" character.
When a carriage return character (0x0d) is needed in a string it is entered as "\r". Also common are the two character sequences line feed "\n" (0X0a), horizontal tab "\t" (0X09) and finally backslash "\" (0x5c).
The backslash case:
NSString *stri = #"\\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:#"\\" withString:#"123"];
NSLog(#"str: %#", str);
NSLog output:
str: 123rdffsdf
NSString *s = #"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: #""];
NSLog(#"%#", s); // => foobarbazfoo
Replace multiple characters in a string in Objective-C?
Since the back slash character is an escape character you have to write two of them.
- (NSString *)sanitizeString:(NSString *)string {
NSCharacterSet* illegalCharacters = [NSCharacterSet characterSetWithCharactersInString:#"\n"];
return [[string componentsSeparatedByCharactersInSet: illegalCharacters] componentsJoinedByString:#"n"];
}

How to right pad a string using stringWithFormat

I would like to be able to right align a string using spaces. I have to be able to use the stringWithFormat: method.
So far I have tried the recommended format and it does not seem to work: [NSString stringWithFormat:#"%10#",#"test"].
I would expect this to return a string that has six spaces followed by "test" but all I am getting is "test" with no spaces.
It appears that stringWithFormat ignores the sizing requests of the %# format specifier. However, %s specifier works correctly:
NSString *test = #"test";
NSString *str = [NSString stringWithFormat:#"%10s", [test cStringUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"'%#'", str);
This prints ' test'.
It's C style formatting. %nd means the width is n.
check following code.
NSLog(#"%10#",[NSString stringWithFormat:#"%10#",#"test"]);
NSLog(#"%#",[NSString stringWithFormat:#" %#",#"test"]);
NSLog(#"%10#", #"test");
NSLog(#"%10s", [#"test" cStringUsingEncoding:[NSString defaultCStringEncoding]]);
NSLog(#"%10d", 1);
NSString *str = #"test";
int padding = 10-[str length]; //6
if (padding > 0)
{
NSString *pad = [[NSString string] stringByPaddingToLength:padding withString:#" " startingAtIndex:0];
str = [pad stringByAppendingString:str];
}
NSLog(#"%#", str);

Resources