I have been planning the route as a custom in google maps for iOS.
How do I parsing the incoming JSON in LINESTRING??
My LINESTRING:
"coordInfo": "LINESTRING (28.646751729297 40.9993029074749, 28.6470087874434 40.9995465119554, 28.6470087874434 40.9995465119554, 28.6474633603416 41.0000088561426)"
},
It looks like, from what you posted, that objectForKey#"coordInfo" gives you a single string with numbers in parentheses. You could parse that using the NSString method componentsSeparatedByCharactersInSet: passing a set that contains left and right parentheses, comma, and space to produce an array of the individual number strings (as well as the word "LINESTRING" as the first string in the array). The array will also contain some empty strings where 2 separator characters are together (like comma and space), so you'll have to test for that when taking objects out of the array.
You could also use an NSScanner like this:
NSString *toParse = #"LINESTRING (28.646751729297 40.9993029074749, 28.6470087874434 40.9995465119554, 28.6470087874434 40.9995465119554, 28.6474633603416 41.0000088561426)";
NSScanner *scanner = [NSScanner scannerWithString:toParse];
double num;
while (! [scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanDouble:&num];
// put numbers into an array here or use them somehow
NSLog(#"%f",num);
}
Related
I'm wondering, is there a way to detect a character that takes up more than 1 index spot in an NSString? (like an emoji). I'm trying to implement a custom text view and when the user pushes delete, I need to know if I should delete only the previous one index spot or more.
Actually NSString use UTF-16.So it is quite difficult to work with characters which takes two UTF-16 charater(unichar) or more.But you can do with rangeOfComposedCharacterSequenceAtIndexto get range and than delete.
First find the last character index from string
NSUInteger lastCharIndex = [str length] - 1;
Than get the range of last character
NSRange lastCharRange = [str rangeOfComposedCharacterSequenceAtIndex: lastCharIndex];
Than delete with range from character (If it is of two UTF-16 than it deletes UTF-16)
deletedLastCharString = [str substringToIndex: lastCharRange.location];
You can use this method with any type of characters which takes any number of unichar
For one you could transform the string to a sequence of characters using [myString UTF8String] and you can then check if the character has its first bit set to one or zero. If its one then this is a UTF8 character and you can then check how many bytes are there to this character. Details about UTF8 can be found on Wikipedia - UTF8. Here is a simple example:
NSString *string = #"ČTest";
const char *str = [string UTF8String];
NSMutableString *ASCIIStr = [NSMutableString string];
for (int i = 0; i < strlen(str); ++i)
if (!(str[i] & 128))
[ASCIIStr appendFormat:#"%c", str[i]];
NSLog(#"%#", ASCIIStr); //Should contain only ASCII characters
I am looking for a short and convenient way to extract a product's price from NSString.
I have tried regular expressions, but always found some cases where did not match.
The price can be any number including decimals, 0 and -1 (valid prices: 10, 10.99, -1, 0).
NSString can contain a string like: #"Prod. price: $10.99"
Thanks!
This will match all the examples you have given
-?\d+(\.\d{2})?
Optionally a -, followed by 1-many digits, optionally followed by a decimal point and 2 more digits.
If you've got other numbers that are not prices mixed in to the data then I don't think regex can fulfil your needs.
NSString *originalString = #"Prod. price: $10.99";
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:#"-0123456789"];
[scanner scanUpToCharactersFromSet:numbers intoString:NULL];
double number;
[scanner scanDouble:&number];
number is equal to 10.99
Obviously if you have other numbers before the value you looking for you wont find it.
Assuming that your NSString will always contain the price with $ prep-ended to it, the following regex will match your need
.*?\$(-?(\d+)(.\d{1,2})?)
Once the above regex is matched you can find out match.group(1) to be the price from the NSString.
So I imagine that I need a regex statement to do this but I haven't had to do any regex with objective c yet, and I haven't written a regex statement in like a year.
I think it should be like ((?=.*[0-9]).{7,1000})
How do I put this into an objective c string comparison and evaluate the results?
Also is my regex correct?
While a regular expression would probably work, there is another approach:
NSString *str = // some string to check for at least one digit and a length of at least 7
if (str.length >= 7 && [str rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound) {
// this matches the criteria
}
This code actually checks more than just 0-9. It handles digits from other languages too. If you really just want 0-9 then replace:
[NSCharacterSet decimalDigitCharacterSet]
with:
[NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
I have a NSString in this format:
"Key1-Value1,Key2-Value2,Key3-Value3,..."
I need only keys (with a space after every comma):
Key1, Key2, Key3, etc.
I thought to create an array of components from the string using the comma as separator, and after, for every component, extract all characters since the "-"; then I'd serialize the array elements. But I fear this could be very heavy about performances.
Do you know a way to do this using regular expressions?
The regex will greatly depend on the data you are using. For example if the key or value is allowed to be all numbers, or allowed to contain space and punctuation, you would need to modify the regex. For your current example however this will work.
NSString *example = #"Key1-Value1,Key2-Value2,Key3-Value3,...";
NSString *result = [example stringByReplacingOccurrencesOfString:#"(\\w+)-(\\w+),?"
withString:#"$1, "
options:NSRegularExpressionSearch
range:NSMakeRange(0, [example length])];
result = [result stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#", "]];
NSLog(#"%#", result);
I am making an app where there is a requirement to store ASCII art into a database. I store the strings in the below format.
"___________\n |---------|-O\n/___________\\n|______________|\n\____________/"
When I retrieve the data and display it in a label, I want the newline characters and backslashes to be parsed so as to display the real shape of the ASCII art.
How should I parse this kind of strings?
NSString has a method to do what you want, which is to replace a litteral \n, with a newline character (which is symbolized as \n). In a c-format string you can use a double slash to let the library know the second slach is a real one and not an escape symbol. So this should work assuming you have been able to load your data from sqlite into an NSString:
newString = [yourStringFromSQLite stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
If you are using \n just for creating new lines then instead of that just keep space while inserting value in database and set following properties for label ->
1)keep width just to fit first word.
2)linebreakmode to wordwrap (so as width will not be available it will wrap next word to new line)
3)set no. of lines to 0
Hope this will help.
try to use the scanner for remove the html entities
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:#"<" intoString:NULL] ;
[theScanner scanUpToString:#">" intoString:&text] ;
html = [html stringByReplacingOccurrencesOfString:[ NSString stringWithFormat:#"%#>", text] withString:#" "];
}
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
and call this method where your trimmed string need to display