I have a word i.e., P roduct Name in a textfield . we have to trim the space between "P" and "r".
Can you please suggest the solution for this.
Thanks in advance.
There is no real reliable solution for this - there is not enough information provided.
(Unless of course you iterated through a word list, which would be, well, horribly inefficient)
P and r could be separate words for all the computer cares..
This can help you to remove spaces between two words.
NSString *string = #"P roduct Name";
NSString *secondString = [string stringByReplacingOccurrencesOfString:#" " withString:#""];
Output: ProductName
If You want to remove white spaces at start and end of your string
NSString *s = #" Product Name ";
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Output: 'Product Name'
Related
In textfield when user enter value with escape character, I want to work this same as user enter but it behaves like a string for \e too.
In more detail.
suppose user have entered \e[3~ but when i print it in console it shows \e[3~. Actually this should print like ¿[3~ so it will work perfect for me.
I have tried by this line and it works.
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
But suppose if user enter value in caps like \E[3~ then its not replacing. so i have tried with like
NSString * str=[textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str=[textField.text stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
But this is not working in case while user have entered text in small \e[3~. With this when first line will execute to replace \e it will give me perfect result like ¿[3~ but when it will execute next line it gives me same \e[3~ string.
Please suggest me how can i check for both letters and if i can check for all escaping characters at once.
The error in your code is that the second line takes the original string textField.text
instead of the result from the first line. What you probably meant is:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e" withString:#"\e"];
str = [str stringByReplacingOccurrencesOfString:#"\\E" withString:#"\E"];
Alternatively, you can do the replacement in a single step by using the
NSCaseInsensitiveSearch option:
NSString *str = [textField.text stringByReplacingOccurrencesOfString:#"\\e"
withString:#"\e"
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [textField.text length])];
I have an NSString and want to save it in CSV format. Because of this, I am replacing all " with "". I tried the following code but the compiler reports an error for it.
newString = [oldstring stringByReplacingOccurrencesOfString:#""" withString:#""""];
where oldstring is 11th ave, "L&T", Khar. oldstring is set from user input.
How can I end up with newString set to 11th ave, ""L&T"", Khar?
Any help will be appreciated.
You need to escape double quotes (") inside your string:
newString = [oldString stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
This code SHOULD clean phone number, but it doesn't:
NSLog(#"%#", self.textView.text);
// Output +358 40 111 1111
NSString *s = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", s);
// Output +358 40 111 1111
Any ideas what is wrong? Any other ways to remove whitespacish characters from text string (except the hard way)?
Try this
NSCharacterSet *dontWantChar = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *string = [[self.textView.text componentsSeparatedByCharactersInSet:dontWantChar] componentsJoinedByString:#""];
The documentation for stringByTrimmingCharactersInSet says:
Returns a new string made by removing from both ends of the
receiver characters contained in a given character set.
In other words, it only removes the offending characters from before and after the string any valid characters. Any "offending" characters are left in the middle of the string because the trim method doesn't touch that part.
Anyways, there are a few ways to do the thing you're trying to do (and #Narayana's answer is good on this, too... +1 to him/her). My solution would be to set your string s to be a mutable string and then do:
[s replaceOccurrencesOfString: #" " withString: #"" options: NSBackwardsSearch range: NSMakeRange( 0, [s length] )];
I was wondering how to add whitespaces inbetween letters/numbers in a string with Objective-C.
I have the sample code kinda working at the moment. Basically I want to turn "West4thStreet" into "West 4th Street".
NSString *myText2 = #"West4thStreet";
NSString *regexString2 = #"([a-z.-][^a-z .-])";
for(NSString *match2 in [myText2 componentsMatchedByRegex:regexString2 capture:1L]) {
NSString *myString = [myText2 stringByReplacingOccurrencesOfString:match2 withString:#" "];
NSLog(#"Prints out: %#",myString); // Prints out: Wes thStreet // Prints out: West4t treet
}
So in this example, it's replacing what I found in regEx (the "t4" and "hS") with spaces. But I just want to add a space inbetween the letters to separate out the words.
Thanks!
If you wrap parts of your regex patterns in parentheses, you can refer to them as $1, $2, etc in your replacement string (patterns are numbered from left to right, by the order of their opening parenthesis).
NSString *origString = #"West4thStreet";
NSString *newString = [origString stringByReplacingOccurrencesOfRegex:#"(4th)" withString:#" $1 "];
Not sure I understand your broader use case, but that should at least get you going...
How can I encode convert spaces to   in objective-c?
I need each space is replaced by  , except when only one space
Example
in text: AAA BBB C
out text: AAAnbsp;nbsp;nbsp;nbsp;BBB C
thanks
Assuming you're talking about NSStrings -- look at the NSString documentation. There are instance methods for replacing strings of the form stringByReplacing...
Given that you use NSString or NSMutableString:
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#" "];
Yes, this needs to be done twice for the case of an odd number of spaces in a row.
If you are dealing with C-strings instead of NSStrings or NSMutableStrings, you do the same but with a function instead of a method. I am not going to write such a function here but know that you have to run it twice. :)