How to parse value from a url in iOS? - ios

I have a url :
DBSession url : db-qf8erkjgfnhno://1/connect?oauth_token_secret=7ek3fhnfnhla&state=58444830-7764-4DBE-A4F1-84B8604sv2C5&oauth_token=c93dvvaa5b47&uid=2841089
I want to take the value of oauth_token_secret and oauth_token. I tried with [url query] method and get oauth_token_secret=7ek3fhnfnhla&state=58444830-7764-4DBE-A4F1-84B8604sv2C5&oauth_token=c93dvvaa5b47&uid=2841089, but i can not find the method to get the value of oauth_token_secret and oauth_token.
Please help me out.

You can parse query parameters into NSMutableDictionary, and after that retrieve values from there.
NSString *query = [url query];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *param in [query componentsSeparatedByString:#"&"]) {
NSArray *elts = [param componentsSeparatedByString:#"="];
if([elts count] < 2) continue;
[params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];
}
NSLog(#"oauth_token_secret : %#", params[#"oauth_token_secret"]);
NSLog(#"oauth_token : %#", params[#"oauth_token"]);

This is a function that returns string between two characters. This is return you oath token. Change it according to your need.
-(NSString*)contentsInStrings:(NSString *)str
{
NSString *subString = nil;
NSRange range1 = [str rangeOfString:#"&oauth_token="];
NSRange range2 = [str rangeOfString:#"&"];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
{
NSRange range3;
range3.location = range1.location+1;
range3.length = (range2.location - range1.location)-1;
subString = [str substringWithRange:range3];
}
return subString;
}

Related

How to parse a NSString

The string is myAgent(9953593875).Amt:Rs.594 and want to extract 9953593875 from it. Here is what I tried:
NSRange range = [feDetails rangeOfString:#"."];
NSString *truncatedFeDetails = [feDetails substringWithRange:NSMakeRange(0, range.location)];
NSLog(#"truncatedString-->%#",truncatedFeDetails);
This outputs: truncatedString-->AmzAgent(9953593875)
Or you do like this:
NSString *string = #"myAgent(9953593875).Amt:Rs.594.";
NSRange rangeOne = [string rangeOfString:#"("];
NSRange rangeTwo = [string rangeOfString:#")"];
if (rangeOne.location != NSNotFound && rangeTwo.location != NSNotFound) {
NSString *truncatedFeDetails = [string substringWithRange:NSMakeRange(rangeOne.location + 1, rangeTwo.location - rangeOne.location - 1)];
NSLog(#"%#",truncatedFeDetails);
}
do like
Step-1
// split the string first based on .
for example
NSString *value = #"myAgent(9953593875).Amt:Rs.594.How I get 9953593875 only";
NSArray *arr = [value componentsSeparatedByString:#"."];
NSString * AmzAgent = [arr firstObject]; // or use [arr firstObject];
NSLog(#"with name ==%#",AmzAgent);
in here u get the output of myAgent(9953593875)
Step-2
in here use replace string like
AmzAgent = [AmzAgent stringByReplacingOccurrencesOfString:#"myAgent("
withString:#""];
AmzAgent = [AmzAgent stringByReplacingOccurrencesOfString:#")"
withString:#""];
NSLog(#"final ==%#",AmzAgent);
finally you get output as 9953593875
Try this
NSString *str = #"myAgent(9953593875).Amt:Rs.594.";
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:#"(?<=\\()\\d+(?=\\))"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSString *result = [str substringWithRange:[regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])].range];
//result = 9953593875

Fetching data from SQLite and want to get only the last value of column id

I am fetching data from SQLite and want to get only the last value of column id in XCode.The code is
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: #"#lastObject"];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",custval,"1"];
NSLog(#"%#", imgval1);
Please tell me how can I get only the value because by using the above code I am getting array with last value of id.
I think this your case, try this it maybe help you
NSArray *temp=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(#"Dictionary is---->%#",[finalStr lastObject]);
Output:
3_1
EDIT
NSArray *temp=[NSArray arrayWithObjects:#"(1)",#"(2)",#"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
final=[final stringByReplacingOccurrencesOfString:#")" withString:#""];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",final,"1"];
NSLog(#"%#", imgval1);
I don't know is this correct way or not try this....otherwise have look this link
I don't fully understand your code structure hehe. Try this:
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat#"%#",custval];
str = [str stringByReplacingOccurrencesOfString:#"("
withString:#""];
NSString *finalCustval = [NSString stringWithFormat#"%#",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:#")"
withString:#""];
*
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",finalCustval ,"1"];
NSLog(#"%#", imgval1);
UPDATE
try adding the ones with *.

IOS caseInsensitiveCompare

I have the code below that compares two values and returns if match is found, the problem is that it is case-sensitive, I googled around and found the method caseInsensitiveCompare, please help me run this program with caseInsensitiveCompare method, I am lost.
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"person2";
NSRange match = [listOfnames rangeOfString:name];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}
Use the rangeOfString:options: rendition with the NSCaseInsensitiveSearch option:
NSString *listOfnames = #"person, person1, person2, person3";
NSString *name = #"PERSON2";
NSRange match = [listOfnames rangeOfString:name options:NSCaseInsensitiveSearch];
if(match.location == NSNotFound ){
NSLog(#"Person not found!");
}else{
NSLog(#"Found you");
}

iOS : Get substring from a html tag response

In my program I'm getting an html notification tag use
"<script>p(34,'1','hello world');</script>/r/n"
I need now is to get the string "hello world" from above tag and it should be a dynamic process as the length of text varies for every response tag.
Something like this will work. You may have to adjust the fromString and toString if those parts of the message are also dynamic, but this will get you started.
NSString *someString = #"<script>p(34,'1','hello world');</script>/r/n";
NSString *fromString = #"<script>p(34,'1','";
NSRange fromRange = [someString rangeOfString:fromString];
NSString *toString = #"');</script>/r/n";
NSRange toRange = [someString rangeOfString:toString];
NSRange range = NSMakeRange(fromRange.location + fromRange.length, toRange.location - (fromRange.location + fromRange.length));
NSString *result = [someString substringWithRange:range];
Please try to use this one.... I Hope this work will fine..
NSString *data = #"<script>p(34,'1','hello world');</script>/r/n";
NSRange divRange = [data rangeOfString:#"','" options:NSCaseInsensitiveSearch];
if (divRange.location != NSNotFound)
{
NSRange endDivRange;
endDivRange.location = divRange.length + divRange.location;
endDivRange.length = [data length] - endDivRange.location;
endDivRange = [data rangeOfString:#"'" options:NSCaseInsensitiveSearch range:endDivRange];
if (endDivRange.location != NSNotFound)
{
divRange.location += divRange.length;
divRange.length = endDivRange.location - divRange.location;
NSLog(#"SubString %#",[data substringWithRange:divRange]);
}
}
NSString * myString = #"<script>p(34,'1','hello world');</script>/r/n";//#" hello(1234)";
NSRange range1 = [myString rangeOfString:#"," options:NSBackwardsSearch];
NSRange range2 = [myString rangeOfString:#"'" options:NSBackwardsSearch];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location))
{
NSRange range3;
range3.location = range1.location+2;
range3.length = (range2.location - range1.location)-2;
NSString *subString = [myString substringWithRange:range3];
NSLog(#"%#",subString)
}
outPut----> hello world

Unable to extracting a substring from an NSString

I have an input string in the format
"Jerry Lane"(angle bracket)jerry.lane#gmail.com(bracket closed),"Harry Potter"(angle bracket)harry.potter#gmail.com(bracket closed),"Indiana Jones",(angle bracket)indiana.jones#gmail.com(bracket closed),"Tom Cruise"(angle bracket)tom.cruise#gmail.com(bracket closed)
Here, i am supposed to first separate the string on the basis of comma delimiter, which would give me a separate string like
"Jerry Lane"(angle bracket)jerry.lane#gmail.com(bracket closed)
Then i need to save extract the string between the <> brackets, which is essentially the string "jerry.lane#gmail.com". I am using the following code, but it is giving me the following error:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString substringWithRange:]: Range or index out of bounds'
-(NSArray *)parseString:(NSString *)string
{
if(string)
{
NSArray *myArray = [string componentsSeparatedByString:#","];
for(NSMutableString *myString in myArray)
{
NSRange start,end;
start = [myString rangeOfString:#"<"];
end = [myString rangeOfString:#">"];
if(start.location != NSNotFound && end.location != NSNotFound)
{
NSString *emailAddress = [myString substringWithRange:NSMakeRange(start.location,end.location)];
NSString *name = [myString substringToIndex:start.location];
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setValue:emailAddress forKey:#"Dhruvil Vyas"];
[testArray addObject:myDictionary];
}
}
}
return testArray;
}
The arguments that substring takes are the start position and the length
Not the start position and the end position.
More Info
borrrden's answer is correct. Here is another way to do this.
-(NSArray *)parseString:(NSString *)string
{
if(string)
{
NSArray *myArray = [string componentsSeparatedByString:#","];
for(NSMutableString *myString in myArray)
{
NSArray *tempNameArray = [myString componentsSeparatedByString:#"<"];
NSString *email = [tempNameArray objectAtIndex:1];
NSArray *tempMailArray = [email componentsSeparatedByString:#">"];
NSString *emailAddress = [tempMailArray objectAtIndex:0];
NSString *name = [tempNameArray objectAtIndex:0];
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setValue:emailAddress forKey:#"Dhruvil Vyas"];
[testArray addObject:myDictionary];
}
}
return testArray;
}

Resources