142

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • 1
    The 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. – EricLeaf Jul 25 '15 at 21:21
  • @EricLeaf where in the question does it say the string is a date? – JeremyP Jul 27 '15 at 08:47

7 Answers7

375
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])
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 1
    Using `[foo firstObject]` might be better in general case because it will properly handle empty array too. – Bobrovsky Feb 18 '14 at 08: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. – JeremyP Jun 26 '14 at 08: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. – EricLeaf Jul 25 '15 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. – JeremyP Jul 27 '15 at 08:46
36

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.

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
  • 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". – Marcos Reboucas Apr 13 '15 at 12:51
9

Its working fine

NSString *dateString = @"10/10/2010";//Date 
NSArray* dateArray = [dateString componentsSeparatedByString: @"/"];
NSString* dayString = [dateArray objectAtIndex: 0];
8

Objective-c:

NSString *day = [@"10/04/2011" componentsSeparatedByString:@"/"][0];

Swift:

var day: String = "10/04/2011".componentsSeparatedByString("/")[0]
aturan23
  • 4,798
  • 4
  • 28
  • 52
l-l
  • 3,804
  • 6
  • 36
  • 42
6

Use [myString componentsSeparatedByString:@"/"]

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
5

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];
}
Community
  • 1
  • 1
  • 5
    Why not just use componentsSeparatedByString directly? – John Gibb Mar 25 '13 at 15:58
  • 1
    because 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! – Khayrattee Wasseem Mar 28 '13 at 07:29
  • 3
    componentsSeparatedByString hasn't changed in something like two decades. Do you wrap all Apple method calls in case they change? You must type a lot. – tooluser May 23 '14 at 20:26
1

Swift 3.0 version

let arr = yourString.components(separatedBy: "/")
let month = arr[0]
Fangming
  • 24,551
  • 6
  • 100
  • 90