I have a string like this: @"10/04/2011"
and I want to save only the "10" in another string. How can I do that?
-
1The best solution would be to use NSDate and NSDateFormatter. Extra credit and better quality code if you actually localize it to the system to perhaps deal with the month/day day/month differences.– EricLeafJul 25, 2015 at 21:21
-
@EricLeaf where in the question does it say the string is a date?– JeremyPJul 27, 2015 at 8:47
7 Answers
NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"];
NSString* firstBit = [foo objectAtIndex: 0];
Update 7/3/2018:
Now that the question has acquired a Swift tag, I should add the Swift way of doing this. It's pretty much as simple:
let substrings = "10/04/2011".split(separator: "/")
let firstBit = substrings[0]
Although note that it gives you an array of Substring
. If you need to convert these back to ordinary strings, use map
let strings = "10/04/2011".split(separator: "/").map{ String($0) }
let firstBit = strings[0]
or
let firstBit = String(substrings[0])
-
1Using
[foo firstObject]
might be better in general case because it will properly handle empty array too. Feb 18, 2014 at 8:52 -
2@Bobrovsky in the general case, it would be better to validate the string to make sure it is a date. This is just an example that answers the specific question. It's also more generalisable to get any of the three date parts.– JeremyPJun 26, 2014 at 8:57
-
Despite the masses of upvotes I have to disagree. First, why do you think the 10 is day instead of month? This sort of wrong headed approach is why we had the Y2K problem.– EricLeafJul 25, 2015 at 21:20
-
2@EricLeaf The question says "I only want to save the 10 in a string". I could have called the variable anything. The answer is correct for the question. In fact, nowhere does the question say it is a date, so I have amended the answer.– JeremyPJul 27, 2015 at 8:46
Either of these 2:
NSString *subString = [dateString subStringWithRange:NSMakeRange(0,2)];
NSString *subString = [[dateString componentsSeparatedByString:@"/"] objectAtIndex:0];
Though keep in mind that sometimes a date string is not formatted properly and a day ( or a month for that matter ) is shown as 8, rather than 08 so the first one might be the worst of the 2 solutions.
The latter should be put into a separate array so you can actually check for the length of the thing returned, so you do not get any exceptions thrown in the case of a corrupt or invalid date string from whatever source you have.
-
that's correct, just note that in subStringWithRange method the second "S" should not be "lower case" (substringWithRange) or you will get an error like "this method doesn't exist". Apr 13, 2015 at 12:51
Its working fine
NSString *dateString = @"10/10/2010";//Date
NSArray* dateArray = [dateString componentsSeparatedByString: @"/"];
NSString* dayString = [dateArray objectAtIndex: 0];
Objective-c:
NSString *day = [@"10/04/2011" componentsSeparatedByString:@"/"][0];
Swift:
var day: String = "10/04/2011".componentsSeparatedByString("/")[0]
I have formatted the nice solution provided by JeremyP above into a more generic reusable function below:
///Return an ARRAY containing the exploded chunk of strings
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
return [stringToBeExploded componentsSeparatedByString: delimiter];
}
-
5
-
1because I'm using it across several projects, so if anything changes, I just have to change the body of the wrapper function instead of at several places. But if you are just using it once, so yes it makes more sense using it directly Cheers! Mar 28, 2013 at 7:29
-
3componentsSeparatedByString hasn't changed in something like two decades. Do you wrap all Apple method calls in case they change? You must type a lot.– tooluserMay 23, 2014 at 20:26
Swift 3.0 version
let arr = yourString.components(separatedBy: "/")
let month = arr[0]