Extract a Part of the String from Substring - ios

I am trying to find the Regular expression to extract a part of my string from the Entire sub string .Feel free to suggest me a best Way .
so here is the case , i can get any Eamil so lets say abd#gmail.com , or something similar ,
i want to remove just the Username that is "abd" and save into another string.

I don't think you'll need regular expressions for that. Just do this:
NSString* email = #"abd#gmail.com";
NSArray * components = [email componentsSeparatedByString:#"#"];
NSString* username = (NSString *)[components objectAtIndex:0];
you alsa can do:
NSString* email = #"abd#gmail.com";
NSString* username = [email substringToIndex:[email rangeOfString:#"#"].location];

Related

How to perform Localization using sqlite stored data-iOS App?

In my app i am doing localization to support mutiple languages. For that i am creating .string file and using the following method:
-(NSString*) languageSelectedStringForKey:(NSString*)key;
I am using the Key_value concept for this. its working fine. But My question is:
Let say i have a sqlite database which has all the different languages string which needs to be localized.
For eg: for spanish
"Username" = "nombre de usuario";
"Password" = "contraseƱa";
"Submit" = "presentar";
for french
"Username" = "nom d'utilisateur";
"Password" = "mot de passe";
"Submit" = "soumettre";
all this is insert in a sqlite database.
How can we perform localization with the app that way?
Since you seem to have all strings traduction within one db, you need to have an intermediate data structure that will act as a bridge between the db and your method to localize the string.
You could make use of a dictionary for e.g to store the key string identifier and as a value an array that contain all the languages traduction. You will need to feed this data structure once then.
Note that the recommended way is to use .strings files for localisation so you need to provide us good raison why you can't take this approach.
If those are titles/texts for UIButtons and UILabels use the following syntax in your viewController:
NSString *buttonString = NSLocalizedStringFromTable(#"viewController-title-button-username", #"Localizable", nil);
self.username_label.text = buttonString;
And in your Localizable.strings file you do(you must add localization to your project and set this file to be localized to spanish):
"viewController-title-button-username" = "nombre de usuario";
"tabbar-item1" = "item1 in spanish";
"tabbar-item2" = "item2 in spanish";
"tabbar-item3" = "item3 in spanish";
And the same for the Localizable.strings you have set to be the french:
"viewController-title-button-username" = "nom d'utilisateur";
"tabbar-item1" = "item1 in french";
"tabbar-item2" = "item2 in french";
"tabbar-item3" = "item3 in french";
For your tabbar name, put this in your viewDidLoad for each viewController you have. Just change 1, 2, 3 etc.:
NSString *name = NSLocalizedStringFromTable(#"tabbar-item1", #"Main", nil);
self.title = name;
On the other hand if you are getting either an .xml or retrieving using REST you can do the following:
//Get the name of what you want eg. spanish-data or french-data
NSString *name = NSLocalizedStringFromTable(#"xml-locations-string", #"Main", nil);
//What to add at the end
NSString *append = #".xml";
//Merge strings to what you want to retrieve: spanish-data.xml
NSMutableString *file = [NSMutableString stringWithFormat:#"%#%#", name, append];
//From which URL
NSString *urlString = #"http://www.myDomain.com/ProjectName/";
//Create full URL http://www.myDomain.com/ProjectName/spanish-data.xml
NSString *fileUrl = [NSString stringWithFormat:#"%#%#", urlString, file];
NSURL *url = [NSURL URLWithString:fileUrl];
The data you get back will now be the localized version. And you can use that to set table data or whatever you need.
This is the super simple and therefor not optimized way of doing it. Works for REST as well, you just need to reconstruct to fit your project.

NSString find and replace html tags attgibutes

I have pretty simple NSString
f.e
<scirpt attribute_1="x" attribute2="x" attribute3="x" ... attributeX="x"/>
I need to find specific parameter, let's say attribute2 and replace it's value,
I know the exact name of parameter f.e attribute2 but I don't know anything about it's value.
I guess it can be easily done by regexp, but I quite newbie on it.
In conclusion: I want to grab
attribute2="xxxx...xxx"
from incoming string
Note: I don't want to use some 3rd party libs to achieve that (it's temporary hack)
Any help is appreciated
I hacked it together with some string operations:
NSDictionary* valuesToReplace = #{#"attribute_1" : #"newValue1"};
NSString* sourceHtml = #"<scirpt attribute_1=\"x\" attribute2=\"x\" attribute3=\"x\" attributeX=\"x\" />";
NSArray* attributes = [sourceHtml componentsSeparatedByString:#" "];
for (NSString* pair in attributes)
{
NSArray* attributeValue = [pair componentsSeparatedByString:#"="];
if([attributeValue count] > 1) //to skip first "script" element
{
NSString* attr = [attributeValue firstObject]; //get attribute name
if([valuesToReplace objectForKey:attr]) //check if should be replaced
{
NSString* newPair = [NSString stringWithFormat:#"%#=\"%#\"", attr, [valuesToReplace objectForKey:attr]]; //create new string for that attribute
sourceHtml = [sourceHtml stringByReplacingOccurrencesOfString:pair withString:newPair]; //replace it in sourceHtml
}
}
}
It's really only when you want to hack it and you know the format :) Shoot a question if you have any.
you can try this.. https://github.com/mwaterfall/MWFeedParser
import "NSString+HTML.h" along with dependencies
And write like this...
simpletxt.text = [YourHTMLString stringByConvertingHTMLToPlainText];

NSString separation-iOS

I have following strings. But I need to separate them by this "jsonp1343930692" and assign them NSString again. How could I that? I could able to separate them to NSArray but I don't know how to separate to NSString.
jsonp1343930692("snapshot":[{"timestamp":1349143800,"data":[{"label_id":10,"lat":29.7161,"lng":-95.3906,"attr":{"ozone_level":37,"exp":"IN","gridpoint":"29.72:-95.39"}},{"label_id":10,"lat":30.168456,"lng":-95.50448}]}]})
jsonp1343930692("snapshot":[{"timestamp":1349144700,"data":[{"label_id":10,"lat":29.7161,"lng":-95.3906,"attr":{"ozone_level":37,"exp":"IN","gridpoint":"29.72:-95.39"}},{"label_id":10,"lat":30.168456,"lng":-95.50448,"attr":{"ozone_level":57,"exp":"IN","gridpoint":"30.17:-95.5"}},{"label_id":10,"lat":29.036944,"lng":-95.438333}]}]})
The jsonp1343930692 prefix in your string is odd: I don't know where you string come from, but it really seems to be some JSON string with this strange prefix that has no reason to be there. The best shot here is probably to check if it is normal to have this prefix, for example if you get this string from a WebService it is probably the WebService fault to return this odd prefix.
Anyway, if you want to remove the jsonp1343930692 prefix of your string, you have multiple options:
Check that the prefix is existant, and if so, remove the right number of characters from the original string:
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
if ([str hasPrefix:kStringToRemove])
{
// rebuilt a string by only using the substring after the prefix
str = [str substringFromIndex:kStringToRemove.length];
}
Split your string in multiple parts, using the jsonp1343930692 string as a separator
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
NSArray* parts = [str componentsSeparatedByString:kStringToRemove];
str = [parts componentsJoinedByString:#""];
Replace every occurrences of jsonp1343930692 by the empty string.
NSString* str = ... // your string with the "jsonp1343930692" prefix
static NSString* kStringToRemove = #"jsonp1343930692";
str = [str stringByReplacingOccurrencesOfString:kStringToRemove withString:#""];
So in short you have many possibilities depending on what exactly you want to do :)
Of course, once you have removed your strange jsonp1343930692 prefix, you can deserialize your JSON string to obtain a JSON object (either using some third-party lib like SBJSON or using NSJSONSerializer on iOS5 and later, etc)
Have a look at the NSJSONSerialization class to turn this into a Cocoa collection that you can deal with.

ios - problems after encoding a url string

I have some code to send a url to a remote server. If I do not encode the url, it works perfectly. But if I encode the url, it does not work. So I am pretty sure something is not right with the way I encode the url query string.
Here is my code:
// URL TO BE SUBMITTED.
NSString *urlString =
#"http://www.mydomain.com/test.php?";
// NOW CREATE URL QUERY STRING
NSString *unencoded_query_string =
#"name=%#&user_id=%#&person_name=%#&person_email=%#&privacy=%#";
// PUT PREVIOUSLY SET VALUES INTO THE QUERY STRING
NSString *unencoded_url_with_params =
[NSString stringWithFormat:unencoded_query_string, business , user_id , name , email , privacy_string];
// ENCODE THE QUERY STRING
NSString *escapedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencoded_url_with_params,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
// NOW APPEND URL TO QUERY STRING
NSString *full_encoded_url_string =
[urlString stringByAppendingString: escapedString];
and then I send this string to the server, and the server does have the correct request file invoked, but isn't able to read the parameters.
Would anyone know what I doing incorrectly here? I am using arc by the way.
Thanks!
I think you probably want to escape each param, not the entire request. Basically you want to escape ampersands, spaces etc that show up in your get variables. Your encoded URL probably looks like this:
http://www.mydomain.com/test.php?name%3DPeter%20Willsey%26user_id%3DUSERID%26person_name%3DPeter%20Willsey%26person_email%3Dpeter%40test.com%26privacy%3D1
and it should look like this:
http://www.mydomain.com/test.php?name=Peter%20Willsey&user_id=25&person_name=Peter%20Willsey&person_email=peter%40test.com&privacy=1

Search NSString in line from file

Is it possible to make a function that searchs a string for an exact substring so that it will only 'return true' if the exact string if found, not as part of a larger word?
NSString* search = #"tele";
NSString* stringOne = #"telephone";
NSString* stringTwo = #"tele phone";
NSString* stringThree = #"phone tele";
What I mean is: Is it possible to search for a string in a way that the NSString 'search' would be found in strings Two and Three, but not One?
Try using the following function in the NSString class:
- (NSRange)rangeOfString:(NSString *)aString
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html
Simplest approach is to append blanks (or whatever your separators are) to front and rear of both strings, then do the search.

Resources