如何在 Objective C 中分割字符串

发布于 2024-10-19 01:31:31 字数 130 浏览 2 评论 0原文

如何在 Objective-C 中分割字符串?我正在开发一个包含日期选择器的简短应用程序。我确实显示日期从日期选择器获取并通过标签显示它。

我的主要问题是如何将日期拆分为三个单独的字符串? 有人对此有想法吗?

谢谢

How to split a string in objective-C? I am working on an short application that contains a date picker. I do display date get it from date picker and display it through a label.

My main question is that how can I split the the date in to three separate strings?
any one has idea about it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小女人ら 2024-10-26 01:31:31

您可以使用 -[NSString ComponentsSeparatedByString:]NSScanner 来分割字符串,或使用 NSCalendar 提取您感兴趣的日期。

You could use -[NSString componentsSeparatedByString:] or NSScanner to split the string, or use NSCalendar to extract the pieces of the date you're interested in.

雨巷深深 2024-10-26 01:31:31

使用类似 [yourString ComponentsSeparatedByString:@"/"] 的内容。你将得到一个由分隔字符串组成的 NSArray。

Use something like [yourString componentsSeparatedByString:@"/"]. You will get an NSArray of separated strings.

人间不值得 2024-10-26 01:31:31

如果可以使用 NSDateFormatter 将日期格式化为特定格式,然后将 NSDate 转换为 NSString。您可以使用 ComponentsSeparatedByString 从日期字符串中提取组件。

NSDate *myDate = datePicker.date;//if you are getting date from date picker

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];//you can use any date format also
NSString *myDateString = [dateFormat stringFromDate:myDate];

您可以使用此方法获取字符串数组,并且可以根据您的日期格式使用字符串标记生成器。例如“,”字符串标记生成器。

[myDateString componentsSeparatedByString:@","];

If you can format date into specific format using NSDateFormatter and then convert NSDate into NSString. You can extract components from date string using componentsSeparatedByString.

NSDate *myDate = datePicker.date;//if you are getting date from date picker

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];//you can use any date format also
NSString *myDateString = [dateFormat stringFromDate:myDate];

You can get array of string by using this method and you can use string tokenizer according to your dateformat.for example "," string tokenizer.

[myDateString componentsSeparatedByString:@","];
左耳近心 2024-10-26 01:31:31

您可以使用以下代码将日期拆分为单独的部分:

NSDate *inputDate = [NSDate date];  //assign the value selected from date picker 
NSCalendar* calendar = [NSCalendar currentCalendar];

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* componentObj = [calendar components:unitFlags fromDate:inputDate];
NSInteger day = componentObj.day;
NSInteger month = componentObj.month;
NSInteger year = componentObj.year;

You can use following code to split the date into separate components:

NSDate *inputDate = [NSDate date];  //assign the value selected from date picker 
NSCalendar* calendar = [NSCalendar currentCalendar];

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents* componentObj = [calendar components:unitFlags fromDate:inputDate];
NSInteger day = componentObj.day;
NSInteger month = componentObj.month;
NSInteger year = componentObj.year;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文