String includin Escape character - iphone

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])];

Related

Find words with regEx and then add whitespaces inbetween with Objective-c

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...

NSString removing single quote in string

This should be simple but it's not working. I am trying to strip single quote marks from an NSString named parms using the following (stripped of non-relevant vars in the format string):
NSString *newVar =[[NSString alloc] initWithFormat:#"%#", [parms stringByReplacingOccurrencesOfString:#"'" withString:#""]];
So if parms contains "Mike's Hat" I would expect that newVar would contain "Mikes Hat". Instead it contains "Mike's Hat".
There must be more to your code than you are proving, but the following works perfectly:
NSString *parms = #"Mike's Hat";
NSString *newVar =[parms stringByReplacingOccurrencesOfString:#"’" withString:#""];
NSLog(#"%#",newVar);
Output: Mikes Hat
There could be a possibility that the character ' may not be the same character in your parms string if the above does not work for you.
Turns out, you are using the wrong character copy/paste this character into your string: ’
Just my two cents on this same problem I had in my code.... When I used the single quote on the keyboard to type ' into my code it didn't work. But I was printing string values to the console. When I copied and pasted the ' character from the console into my code it then worked. What is weird is that I'm using the same key on the keyboard to enter the string into a UITextField so I really don't know why the same key gets turned into something different but that's how I solved it.

How trim the spaces given in a word for textField

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'

Splitting a number off prefix of a string on iPhone

Say I have a string like "123alpha". I can use NSNumber to get the 123 out, but how can I determine the part of the string that NSNumber didn't use?
You can use NSScanner to both get the value and the rest of the string.
NSString *input = #"123alpha";
NSScanner *scanner = [NSScanner scannerWithString:input];
float number;
[scanner scanFloat:&number];
NSString *rest = [input substringFromIndex:[scanner scanLocation]];
If it is important to know exactly what is left after parsing the value this is a better approach than trying to trim characters. While I can't think of any particular bad input at the moment that would fail the solution suggested by the OP in the comment to this answer, it looks like a bug waiting to happen.
if your numbers are always at the beginning or end of a string and you want only the remaining characters, you could trim with a character set.
NSString *alpha = #"123alpha";
NSString *stripped = [alpha stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]];
If its starts out as a char * (as opposed to an NSString *), you can use strtol() to get the number and discover where the number ends in a single call.

(iPhone)URL formatting question

I am trying to format a URL but am getting a bug out of it. My code is below.
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%20score%20is:%i%20and%20CharsPerMin%20is:%#", currentScore, charPerMin.text];
When calling the method it doesn't do a thing. I think the issue is with %20. %20 is being used to space each word in the URL.
You need to escape your % signs by doubling them:
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%%20PracticeTyper%%20score%%20is:%i%%20and%%20CharsPerMin%%20is:%#", currentScore, charPerMin.text];