Split an NSString to access one particular piece

Objective CSwiftStringNsstring

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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

Solution 2 - Objective C

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.

Solution 3 - Objective C

Its working fine

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

Solution 4 - Objective C

Objective-c:

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

Swift:

var day: String = "10/04/2011".componentsSeparatedByString("/")[0]

Solution 5 - Objective C

Use [myString componentsSeparatedByString:@"/"]

Solution 6 - Objective C

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

Solution 7 - Objective C

Swift 3.0 version

let arr = yourString.components(separatedBy: "/")
let month = arr[0]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncyclingIsBetterView Question on Stackoverflow
Solution 1 - Objective CJeremyPView Answer on Stackoverflow
Solution 2 - Objective CAntwan van HoudtView Answer on Stackoverflow
Solution 3 - Objective CVinukondaPraveenView Answer on Stackoverflow
Solution 4 - Objective Cl-lView Answer on Stackoverflow
Solution 5 - Objective CJulio GorgéView Answer on Stackoverflow
Solution 6 - Objective CKhayrattee WasseemView Answer on Stackoverflow
Solution 7 - Objective CFangmingView Answer on Stackoverflow