How to get integer value from Sqlite Database? - iphone

I am working with Sqlite Database.
In my database
I have some filed Like, Invoice,Name,Date
My Invoiceno datatype is INTEGER,
In Select query Got output like this.
Query:(
{
InvoiceNo = 1;
Name : ABC
Date = "2012-04-16 00:00:00";
}
)
But when try to Get that Invoice no to string it's given Diff. Value.
Code for getting invoice from array to String:
NSString *str = [NSString stringWithFormat:#"%i",[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"]];
NSLog(#"str:%#",str);
Here, My invoice no is 1, & It's give me a value "2387056"
Please, Help me..How can i solve this problem?

NSString *str = [NSString stringWithFormat:#"%i",[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"]];
NSLog(#"str:%#",str);
you have to replace above lines with below lines
NSString *str = [NSString stringWithFormat:#"%i",[[[Query objectAtIndex:indexPath.row] valueForKey:#"InvoiceNo"] intValue]];
NSLog(#"str:%#",str);

Convert NSString to int
NSString *values = #"1";
int yourValue = [values intValue];
NSLog(#"Int Value : %d",yourValue);

Related

Convert String into special - splitting an NSString

I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"?
the following gives you the desired result:
NSString *_inputString = #"\"mocktail, wine, beer\"";
NSLog(#"input string : %#", _inputString);
NSLog(#"output string : %#", [_inputString stringByReplacingOccurrencesOfString:#", " withString:#"\", \""]);
the result is:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
You need to use:
NSArray * components = [myString componentsSeparatedByString: #", "];
NSString *string = #"mocktail, wine, beer";
//remove whitespaces
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//get array of string
NSArray *array = [trimmedString componentsSeparatedByString:#","];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSString *trimmedString in array) {
NSString *newString = [NSMutableString stringWithFormat:#"'%#'", trimmedString];
[newArray addObject:newString];
}
//merge new strings
NSString *finalString = [NSString stringWithFormat:#"%#", [newArray objectAtIndex:0]];
for (NSInteger i = 1; i < [newArray count]; i++) {
finalString = [NSString stringWithFormat:#"%#, %#", finalString, [newArray objectAtIndex:i]];
}
Without knowing spesifically about iOS or objective-c, I assume you could use a split function.
In almost any higher level programming language there is such a function.
Try:
Objective-C split
This gets you an array of Strings. You can then practically do with those what you want to do, e.g. surrounding them with single quotes and appending them back together. :D

How to get integer values from nsstring Evalution in iPhone?

I have NSString like this #"0,0,0,0,0,0,1,2012-03-08,2012-03-17". I want values separated by comma. I want values before comma and neglect comma.
I am new in iPhone, I know how to do in Java but not getting how to do in Objective-C.
NSString *string = #"0,0,0,0,0,0,1,2012-03-08,2012-03-17";
NSArray *componentArray = [string componentSeperatedByString:#","];
Use the componentsSeparatedByString: method of NSString.
NSString *str = #"0,0,0,0,0,0,1,2012-03-08,2012-03-17";
NSArray *components = [str componentsSeparatedByString:#","];
for (NSString *comp in components)
{
NSLog(#"%#", comp);
}
for (NSString *s in [yourstring componentsSeparatedByString:#","])
{
int thisval = [s intValue];
}

How can I get an integer value from NSString in an iPhone application?

NSString * str=[zoneDict objectForKey:#"name"];
NSLog(#"==========string zone::==========%#",str);
// str="(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
How can I get the 3:00 value from the above string?
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSRange endRange = [str rangeOfString:#")"];
NSString *timeString = [str substringWithRange:NSMakeRange(5, endRange.location-5)];
NSRange separatorRange = [timeString rangeOfString:#":"];
NSInteger hourInt = [[timeString substringWithRange:NSMakeRange(0, separatorRange.location)] intValue];
NSLog(#"Hour:%d",hourInt);
Rather than trying to extract the time offset from the string, is there any way you could store actual time zone data in your zoneDict? For example you could store NSTimeZone instances instead.
If all you have is the string, you could use an NSRegularExpression object and extract the relevant information using a regular expression instead.
If you could explain further what you're trying to do then there may be an alternative way to achieve what you want.
I like to use -[NSString componentsSeparatedByString]:
NSString *str = #"(GMT -3:00) Baghdad, Riyadh, Moscow, St. Petersbur";
NSArray *myWords = [myString componentsSeparatedByString:#")"];
NSString *temp1 = [myWords objectAtIndex:0];
if ([temp1 rangeOfString:#"-"].location == NSNotFound) {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"+"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
else {
NSArray *temp2 = [temp1 componentsSeparatedByString:#"-"];
NSString *temp3 = [temp2 objectAtIndex:1];
NSLog(#"Your String - %#", temp3);
}
Output:
Your String - 3:00
Using regular expressions is the better option in my view (if you are forced to extract the '3' only). The regular expression string would contain something like "\d?" but don't quote me on that, you'll have to look up the exact string. Perhaps someone on here could provide the exact string.

Substring with asterisks in NSString

Hi friends i want to display credit card number in table view cell,so i want to display only last four characters like XXXXXXXXXX1111 first charecters is replaced with x or some other character how can i do this can any one help me
i have tried like bellow
NSString *CardNumber = [CardNumberValues objectAtIndex:indexPath.row];
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
NSMutableCharacterSet *NumSet = [NSMutableCharacterSet characterSetWithRange:subStringRange];
CardNumber = [[CardNumber componentsSeparatedByCharactersInSet:NumSet] componentsJoinedByString:#"X"];
cell.CardNumber.text = CardNumber;
but its not working can u suggest any reference or code...
Use this
NSString *CardNumber = #"12345678901234";
NSString *str_padding=#"";
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
str_padding =[str_padding stringByPaddingToLength:subStringRange.length withString:#"X" startingAtIndex:0];
CardNumber = [CardNumber stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#",[CardNumber substringToIndex:subStringRange.length]] withString:str_padding];
NSLog(#"%#",CardNumber);
NSString *cardNumber = [NSString stringWithFormat: #"XXXX-XXXX-XXXX-%#", [[CardNumberValues objectAtIndex:indexPath.row] subStringWithIndex:12]];
cell.CardNumber.text = cardNumber;
I am assuming that card numbers are 16 digits.

NSRegularExpression to extract text

All,
I have a dictionary with two keys and values. I need to extract bits and pieces from them and place them into seperate strings.
{
IP = "192.168.17.1";
desc = "VUWI-VUWI-ABC_Dry_Cleaning-R12-01";
}
That is what the dictionary looks like when I call description.
I want the new output to be like this:
NSString *IP = #"192.168.17.1";
NSString *desc = #"ABC Dry Cleaning"; //note: I need to get rid of the underscores
NSString *type = #"R";
NSString *num = #"12";
NSString *ident = #"01";
How would I achieve this?
I've read through the Apple developer docs on NSRegularExpression but I find it hard to understand. I'm sure once I get some help once here I can figure it out in the future, I just need to get started.
Thanks in advance.
Okay, so first, you have to get the object associated with each key:
NSString *ip = [dic objectForKey:#"IP"]; //Btw, you shouldn't start a variable's name with a capital letter.
NSString *tempDesc = [dic objectForKey:#"desc"];
Then, what I would do is split the string in tempDesc, based on the character -.
NSArray *tmpArray = [tempDesc componentsSeparatedByString:#"-"];
Then you just have to get the strings or substrings you're interested in, and reformat them as needed:
NSString *desc = [[tmpArray objectAtIndex:2] stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSString *type = [[tmpArray objectAtIndex:3] substringToIndex:1];
NSString *num = [[tmpArray objectAtIndex:3] substringFromIndex:1];
NSString *ident = [tmpArray objectAtIndex:4];
As you can see, this works perfectly without using NSRegularExpression.