开发者

retrieving NSString from array in iPhone

I have an array which contains description of a route on map. I got this array by parsing JSON. My arrays contains string in this format:

    "<b>Sri Krishna Nagar Rd</b> \U306b\U5411\U304b\U3063\U3066<b>\U5317\U6771</b>\U306b\U9032\U3080",


"\U53f3\U6298\U3057\U3066\U305d\U306e\U307e\U307e <b>Sri Krishna Nagar Rd</b> \U3092\U9032\U3080",


        "\U5927\U304d\U304f\U5de6\U65b9\U5411\U306b\U66f2\U304c\U308a\U305d\U306e\U307e\U307e <b>Bailey 开发者_如何学CRd/<wbr/>NH 30</b> \U3092\U9032\U3080<div class=\"\">\U305d\U306e\U307e\U307e NH 30 \U3092\U9032\U3080</div><div class=\"google_note\">\n<b landmarkid=\"0x39ed57bfe47253b7:0x779c8bf48892f269\" class=\"dir-landmark\">Petrol Bunk</b>\U3092\U901a\U904e\U3059\U308b<div class=\"dirseg-sub\">\Uff083.9&nbsp;km \U5148\U3001\U53f3\U624b\Uff09</div>\n</div>",

Now I want to get name of places from this array like Sri Krishna Nagar Rd , NH 30 Petrol Bunk. First two should give Sri Krishna Nagar Rd and last on should give NH 30 Petrol Bunk How can I get result like this.Any help would be appreciated. Thanx In Advance.

Again, suppose I have string in this format..."\U5de6\U6298\U3059\U308b" which don't have ny place name.How will i handle this scenarios.


    You can get like below:


  NSString *strName=[yourArray objectAtIndex:index];

    NSString *yourPlaceString=[[strName componentsSeparatedByString:@"<b>"] objectAtIndex:1];
    yourPlaceString=[[yourPlaceString componentsSeparatedByString:@"</b>"] objectAtIndex:0];

    you can get all places like this.


First of all, you should check if you don't have any other cleaner API available for the service you query this data. If the service returns such garbage in its JSON response, that shouldn't be your responsability to clean up that mess: the service should return some text that is more usable if it is a real clean API.

Next, if you really don't have any other choice and really need to clean this text, you have two options:

  • If the text is XHTML (I mean real XHTML, conforming to the XML standard) you may use an NSXMLParser to filter out any tags and only keep the text from your string. This may be a bit too much for this anyway so I don't really recommand it.
  • You can use regular expressions. If you are developping for iOS4.0+ you can use the NSRegularExpressionclass for this purpose. The tricky part is to get the right regex (can help you with that if needed)
  • You can use the NSScanner class (which is available in iOS since 2.0 IIRC) to scan characters in you string and parse it. This is probably easier to understand and the way to go if you are not a regex expert, so I recommand this approach

For example if you choose the NSScannersolution, you can scan your string for characters in the alphanumeric character set, to scan letters and digits and accumulate it (you may also add ponctuation characters to your NSCharacterSetyou are using if needed). You will have the NSScanner to stop when it encounter characters such as the unicode characters \Uxxxx or like < and >. When you encounter < you can then ask the NSScanner to ignore the characters up to the next >, then start to scan the alphanumeric characters again and accumulating... and so on until the end of the string.


Finally, if you really find a pattern in the response string you are receiving, like if your place names is always between the first <b> and </b> pair (but you have to be sure of that), you can handle it other ways, like:

  • splitting your string using the <b> text as the separator (e.g. componentsSeparatedByString)
  • or asking the rangeOfString for the string <b> and then for string </b> and once you have their position, only extract substringWithRange from your original string to extract only the place name (using rangeOfString will be faster that componentsSeparatedByString because it will stop on the first occurrence found)


It looks like an encoding problem - can you change the encoding of the source or target to a different format. I had similar issues with German ö ä ü characters when UTF-8 was turned off....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜