How to get integer values from nsstring Evalution in iPhone? - 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];
}

Related

how to give space between two string while concatnating

I am concatenating two string using following way I want space between Added Content To My Learning And the second String any idea how to give space thanks
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [firstString stringByAppendingString:secondString];
You can do it like this.
NSString *firstString =#"Added Contnet To My Learning";
NSString *secondString = appDelegate.activity_Description;
appDelegate.activity_Description = [NSString stringWithFormat:#"%# %#", firstString, secondString];
Did you try?
NSString *firstString =#"Added Contnet To My Learning ";
// a space after the first string
Here is all way you can do with string
//1st Way
NSString *finalString = [NSString stringWithFormat:#"%# %#",firstString,secondString];
//2nd Way
NSString *finalString = [firstString stringByAppendingFormat:#" %#",secondString];
//3rd way
NSArray *ary= [NSArray arrayWithObjects:firstString,secondString, nil];
NSString *finalString= [ary componentsJoinedByString:#" "];
Follow this how to concatenate two strings in iphone

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

NSString select part go a string objective-c

I have problem with string. The string shows: ~00000000:termometr2: +26.9 st.C and I want to use only this part: +26.9 st.C in my textfield.text.
Thanks
NSString *fullStr = #"00000000:termometr2: +26.9 st.C";
NSArray *parts = [fullStr componentsSeparatedByString:#" "];
textField.text =[NSString stringWithFormat:#"%#",[parts objectAtIndex:1]];
it might help you:
NSArray *_array = [yourString componentsSeparatedByString:#":"];
[myTextField setText:[_array lastObject]]; // or any other component you want
Just do it this way, using the method stringByReplacingOccurrencesOfString: withString::
NSString *originalString = #"~00000000:termometr2: +26.9 st.C";
NSString *filteredString = [originalString stringByReplacingOccurrencesOfString:#"~00000000:termometr2: " withString:#""];

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.

Get last path part from NSString

Hi all i want extract the last part from string which is a four digit number '03276' i:e http://www.abc.com/news/read/welcome-new-gig/03276
how can i do that.
You can also use
NSString *sub = [#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent];
If you know how many characters you need, you can do something like this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [string substringFromIndex:[string length] - 5];
If you just know that it's the part after the last slash, you can do this:
NSString *string = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [[string componentsSeparatedByString:#"/"] lastObject];
Since *nix uses the same path separators as URL's this will be valid as well.
[#"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]
If you know the length of the number, and it's not gonna change, it can be as easy as:
NSString *result = [string substringFromIndex:[string length] - 4];
If the last part of the string is always the same length (5 characters) you could use this method to extract the last part:
- (NSString *)substringFromIndex:(NSUInteger)anIndex
Use the length of the string to determine the start index.
Something like this:
NSString *inputStr = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *newStr = [inputStr substringFromIndex:[inputStr length]-5];
NSLog(#"These are the last five characters of the string: %#", newStr);
(Code not tested)
NSString *str = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSArray *arr = [str componentSeparatedBy:#"gig/"];
NSString *strSubStringDigNum = [arr objectAtIndex:1];
strSubStringDigNum will have the value 03276
Try this:
NSString *myUrl = #"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *number = [[myUrl componentsSeparatedByString:#"/"] objectAtIndex: 5];