- // OC
- #import <Foundation/Foundation.h>
- int main()
- {
- @autoreleasepool {
-
- NSString *str = @"u49a-78ui-3gy68-3r7t";
-
- NSMutableString *newStr = [NSMutableString string];
-
-
- for (int i = 0; i<str.length; i++) {
-
- // 取出索引的字符
- char c = [str characterAtIndex:i];
-
- // 判断
- if ( c >= '0' && c<= '9' ) {
-
- [newStr appendFormat:@"%c", c];
- }
- }
-
- NSLog(@"%@", newStr);
- }
-
-
- return 0;
- }
- // C语言
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char s[100] = "u49a-78ui-3gy68-3r7t";
-
- size_t length = strlen(s);
-
- char newS[100];
-
- int c = 0;
-
- for (int i = 0; i<length; i++) {
- if (s[i] >= '0' && s[i] <= '9') {
- newS[c] = s[i];
- c++;
- }
- }
-
- printf("%s", newS);
-
-
- return 0;
- }
复制代码 |