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"];
}
Related
I'm using
[NSString stringWithFormat:#"%C",(unichar)decimalValueX];
but I have to call it thousands of times and its simply too slow.
As an alternative I tried this:
sprintf (cString, "%C", (unichar)decimalValueX);
[NSString stringWithCString:cString encoding:NSUTF16StringEncoding];
but no characters are correctly transalted.
If I try UTF8 instead of 16:
sprintf (cString, "%C", (unichar)decimalValueX);
[NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
I get alphanumeric, but I don't get foreign characters or other special characters.
Can anyone explain whats going on? Or how to make stringWithFormat faster?
Thanks!
It seems that the %C format does not work with sprintf and related functions and non-ASCII characters. But there is a simpler method:
stringWithCharacters:length:
creates an NSString directly from a unichar array (UTF-16 code points).
For a single unichar this would be just
NSString *string = [NSString stringWithCharacters:&decimalValueX length:1];
Example:
unichar decimalValueX = 8364; // The Euro character
NSString *string = [NSString stringWithCharacters:&decimalValueX length:1];
NSLog(#"%#", string); // €
Example for multiple UTF-16 code points:
unichar utf16[] = { 945, 946, 947 };
NSString *string3 = [NSString stringWithCharacters:utf16 length:3];
NSLog(#"%#", string3); // αβγ
For characters outside of the "basic multilingual plane" (i.e.
characters > U+FFFF) you would have to use 2 UTF-16 code points
per character (surrogate pair).
Or use a different API like
uint32_t utf32[] = { 128123, 128121 };
NSString *string4 = [[NSString alloc] initWithBytes:utf32 length:2*4 encoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"%#", string4); // 👻👹
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.
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"
I am making a calculator and unable to seperate the input string in corrosponding to operands.
For example : 2*5 - 6 +8/2. I want an array with components 2, 5, 6, 8, 2 so that I can store the oprators also and then sort accordingly. Please help
NSString *str=#"2*5 - 6 +8/2"; // assume that this is your str
// here remove the white space
str =[str stringByReplacingOccurrencesOfString:#" " withString:#""];
// here remove the all special characters in NSString
NSCharacterSet *noneedstr = [NSCharacterSet characterSetWithCharactersInString:#"*/-+."];
str = [[str componentsSeparatedByCharactersInSet: noneedstr] componentsJoinedByString:#","];
NSLog(#"the str=-=%#",str);
the out put is
the str=-=2,5,6,8,2
You can use the method, componentsSeparatedByCharactersInSet:.
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"*-+/"];
NSArray *numbers = [text componentsSeparatedByCharactersInSet:set];
You can get arrays of the operands and operators like so. This assumes the expression is valid, base 10, begins and ends with operands, etc. The expression would then be operands[0], operators[0], operands[1], operators[1], and so on.
NSString *expression = #"2*5 - 6 +8/2";
// Could use a custom character set as well, or -whitespaceAndNewlineCharacterSet
NSCharacterSet *whitespaceCharacterSet = [NSCharacterSet whitespaceCharacterSet];
NSArray *nonWhitespaceComponents = [expression componentsSeparatedByCharactersInSet:whitespaceCharacterSet];
NSString *trimmedExpression = [nonWhitespaceComponents componentsJoinedByString:#""];
// To get an array of the operands:
NSCharacterSet *operatorCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+-/*"];
NSArray *operands = [trimmedExpression componentsSeparatedByCharactersInSet:operatorCharacterSet];
// To get the array of operators:
NSCharacterSet *baseTenCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
NSArray *operators = [trimmedExpression componentsSeparatedByCharactersInSet:baseTenCharacterSet];
// Since expression should begin and end with operands, first and last strings will be empty
NSMutableArray *mutableOperators = [operators mutableCopy];
[mutableOperators removeObject:#""];
operators = [NSArray arrayWithArray:mutableOperators];
NSLog(#"%#", operands);
NSLog(#"%#", operators);
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);