Replace a char into NSString - iphone

I want simply replace all occourrencies of "+" with a blank " " char...
I tried some sample listed here, also used NSSMutableString, but the program crash...
what's the best way to replace a char from another??
thanks

If you want to replace with a mutable string (NSMutableString) in-place:
[theMutableString replaceOccurrencesOfString:#"+"
withString:#" "
options:0
range:NSMakeRange(0, [theMutableString length])]
If you want to create a new immutable string (NSString):
NSString* newString = [theString stringByReplacingOccurrencesOfString:#"+"
withString:#" "];

NSString *firstString = #"I'm a noob at Objective-C", *finalString;
finalString = [[firstString stringByReplacingOccurrencesOfString:#"O" withString:#"0"] stringByReplacingOccurrencesOfString:#"o" withString:#"0"];
Got the code from here!

Related

Remove special characters in NSMutableAttributedString

In my app to show text in CATextLayer(change colors for characters),using NSMutableAttributedString to change colors,i want remove special characters in NSMutableAttributedString to show PopUp view, but didn't know how to remove special characters, to help to solve problem
i want like this type of o/p
"code" to code //in NSMutableAttributedString, not in NSString
to remove such characters you simply write something like this:
NSMutableString* mutableString = ...;
[mutableString replaceOccurrencesOfString:#"\"" withString:#"" options:0 range:NSMakeRange(0, mutableString.length)];
Note that symbol " is written as \" - this one is called an escape sequence.
Here is a list of such special characters in C - http://msdn.microsoft.com/en-us/library/h21280bw(v=vs.80).aspx
Try this...This may help you.
NSMutableString *unfilteredString = #"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
NSMutableString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
You can give try to this may it fits in your code:
NSMutableString *mutableStrng = [NSMutableString stringWithCapacity:1000];
[mutableStrng setString:#"my name is i#pho#ne"];
[mutableStrng replaceOccurrencesOfString:#"#" withString:#"" options:0 range:NSMakeRange(0,
mutableStrng.length)];
NSMutableAttributedString *mutableAtrString = [[NSMutableAttributedString
alloc]initWithString:mutableStrng];

xcode - decode xml output string

NSString *col1 = [aBook.name stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSLog(#"column1: %#", col1); //output:- ABC+Company
This is my xml data output.
It comes with '+' mark. How can i decode this?
Could you please help me?
You mean get the parts that are seperated by the '+' sign? You can do that with:
NSArray *stringArray = [col1 componentsSeparatedByString:#"+"];
[stringArray objectAtIndex:0]; //ABC
[stringArray objectAtIndex:1]; //Company
If you want to remove/replace the '+' sign you can do this:
NSString* string = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#""]; //last string can be empty or have a string value
Hope this helped you!
You can also replace the plus signs with spaces, if that’s what you’re after:
NSString *name = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#" "];

ObjC: how to add escape character so "hello" becomes \"hello\"?

Currently I am using stringByReplacingOccuranceOfString... to replace " with \", but is there any smarter way?
Thanks!
You can do like this,
NSString *str = #"hello";
NSString *escape = #"\\\””; // this adds ***//*** & then ***/"***
NSString *newStr = [NSString stringWithFormat:#"%#%#%#", escape, str, escape];
NSLog(#"%#", newStr);

How to remove whitespace in a string?

I have a string say "Allentown, pa"
How to remove the white space in between , and pa using objective c?
This will remove all space from myString.
NSString *newString = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];
Here is a proper and documented way of removing white spaces from your string.
whitespaceCharacterSet Apple Documentation for iOS says:
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
+ (id)whitespaceCharacterSet
Return Value
A character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
Discussion
This set doesn’t contain the newline or carriage return characters.
Availability
Available in iOS 2.0 and later.
You can use this documented way:
[yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Hope this helps you.
If you need any more help then please let me know on this.
Probably the solution in one of the answers in Collapse sequences of white space into a single character and trim string:
NSString *whitespaceString = #" String with whitespaces ";
NSString *trimmedString = [whitespaceString stringByReplacingOccurrencesOfString:#" " withString:#""];
If you want to white-space and new-line character as well then use "whitespaceAndNewlineCharacterSet" instead of "whitespaceCharacterSet"
NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
NSString *trimmedString = [temp.text stringByTrimmingCharactersInSet:whitespace];
NSLog(#"Value of the text field is %#",trimmedString);
myStr = [myStr stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *sample = #" string with whitespaces";
NSString *escapeWhiteSpaces = [sample stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- (NSString *)removeWhitespaces {
return [[self componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]]
componentsJoinedByString:#""];
}
In my case NSString was added Zero Width Space(i i used some library). so solution worked for me.
NSMutableString *newString=[[newString stringByReplacingOccurrencesOfString:#"\u200B" withString:#""] mutableCopy];
#"\u200B" is Zero width space character value.
Here is the proper way to remove extra whitespaces from string which is coming in between.
NSString *yourString = #"Allentown, pa";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:#"SELF != ''"];
NSArray *parts = [yourString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
yourString = [filteredArray componentsJoinedByString:#" "];
you can use remove function to remove any substring from the string
- (NSString*)remove:(NSString*)textToRemove fromString:(NSString*)input {
return [input stringByReplacingOccurrencesOfString:textToRemove withString:#""];
}
I have tried all the solutions here, none of them could remove the whitespace generated by the Chinese PinYin Input method.
After some debugging, I found this working:
NSString *newString = [myString stringByReplacingOccurrencesOfString:#"\342\200\206" withString:#""];
I have googled what the '\342\200\206' is, but failed.
Whatever, it works for me.
Hi there is the swift version of the solution with extension :
extension String{
func deleteSpaces() -> String{
return self.stringByReplacingOccurrencesOfString(" ", withString: "")
}
}
And Just call
(yourString as! String).deleteSpaces()
Swift 3:
var word: String = "Hello world"
let removeWhiteSpace = word.stringByRemovingWhitespaces
word = "Helloworld"

How to replace a character in NSString without inserting a space?

Let's assume I have the string
NSString* myString = #"Hello,";
How can I remove the comma without leaving a space? I have tried:
NSString* newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
and
NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
But both are leaving spaces.
I just ran the following as a test
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
And I get 2010-04-05 18:51:18.885 TestString[6823:a0f] Helloxx as output. There is no space left.
NSString *newString = [myString substringToIndex:5];
That will ensure that there are only 5 characters in the string, but beware that this will also throw an exception if there are not at least 5 characters in the string to begin with. How are you handling this string? Is it being displayed to the user? Is it being written to a file? The code that you posted does not reproduce the error, so perhaps you should post the code that you are having a problem with.
the other way u can use.. by stringByReplacingCharactersInRange
token = [token stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"*"];
"token"is your want to replace the NSString and "i" is you want to change NSString by "*"