Extract string upto certain character - iphone

I need to extract string upto certain word
I have time like this :"2012-12-29T00:00:00" how can I extract the part upto TO.That is I dont need time.This string is not static .I mean it changes like "2013-01-21T00:00:00"

use like below
NSString *stingTime = #"2012-12-29T00:00:00";
if([stingTime rangeOfString:#"T"].location != NSNotFound)
stingTime = [stingTime substringToIndex:[stingTime rangeOfString:#"T"].location];
//output
2012-12-29

This should like this...
NSString *string = #"2012-12-29T00:00:00";
NSRange range = [string rangeOfString:#"T0"];
if (range.location != NSNotFound){
NSString *newString = [string substringToIndex:range.location];
NSLog(#"NewString = %#",newString);
}

You can use functions of NSString For e.g.:
NSString *string = #"2012-12-29T00:00:00";
NSString *newString = [string substringToIndex:10];
Your newString will contain 2012-12-29.
Hope this helps.

Related

Replace a character in a String iPhone

I want to replace a single character at a particular position in a string.
Example
String: 123-456-7890
Desired Output: 123-406-7890 (Replacing 5 at fifth position with 0)
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/
visit here and read all about string
Use stringByReplacingCharactersInRange:withString:, forming the NSRange variable to indicate the 5th position.
NSString *phoneNumber = #"123-456-7890";
NSString *newString = [phoneNumber stringByReplacingCharactersInRange:NSMakeRange(5, 1) withString:#"0"];
NSLog(#"%#", newString);
Output: 123-406-7890
Read all about NSString.
for replacing string there are lots of way:
NSString *str = [yourString stringByReplacingOccuranceOfString:#"5" withString:#"0"];
second way first get range of string like:
NSRange range = [yourSting rangeOfString:#"5"];
NSString *first = [yourString substringToIndex:range.location];
NSString *second = [yourString substringFromIndex:range.location+range.length];
NSString *yourNewStr = [NSString stringWithFormat:#"%#0%#",first,second];
Tere are lots of other using string operation but First one is best in that.
Get the range (i.e. index) of first occurrence of the substring.
Then replace at that range with your desired replace value.
NSString *originalString = #"123 456 789";
NSRange r = [originalString rangeOfString:#"5"];
NSString *newString = [originalString stringByReplacingCharactersInRange:r withString:#"0"];
If you want to actually replace the 5th character rather than just any 5 you need to make a range first.
NSRange range = NSMakeRange(5, 1);
NSString *newString = [initialString stringByReplacingCharactersInRange:range withString:#"0"];
Edit: Corrected make range length
you can use :-
NSString *replacechar = #"0";
NSString *newString= [String stringByReplacingCharactersInRange:NSMakeRange(5,1) withString:replacechar];

How do I remove the end of an NSMutableString?

I have the following NSMutableString:
#"1*2*3*4*5"
I want to find the first * and remove everything after it, so my string = #"1"; How do I do this?
NSMutableString *string = [NSMutableString stringWithString:#"1*2*3*4*5"];
NSRange range = [string rangeOfString:#"*"];
if (range.location != NSNotFound)
{
[string deleteCharactersInRange:NSMakeRange(range.location, [string length] - range.location)];
}
You could try to divide this string by a separator and get the first object
NSString *result = [[MyString componentsSeparatedByString:#"*"]objectAtIndex:0];
After calling componentsSeparatedByString:#"*" you'll get the array of strings, separated by *,and the first object is right what you need.
Here's yet another strategy, using the very flexible NSScanner.
NSString* beginning;
NSScanner* scanner = [NSScanner scannerWithString:#"1*2*3*4*5"];
[scanner scanUpToString:#"*" intoString:&beginning];
You could use -rangeOfString: to find the index of the first asterisk and use that with -substringToIndex: to extract a substring from the original input. Something like this perhaps...
NSMutableString *input = #"1*2*3*4*5";
// Finds the range of the first instance. See NSString docs for more options.
NSRange firstAsteriskRange = [input rangeOfString:#"*"];
NSString *trimmedString = [input substringToIndex:firstAsteriskRange.location + 1];

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.

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];