我有以下從服務器值(動態):如何在iPhone上分割字符串?
drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California
現在我想VALU這樣。
drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California
請幫我這個問題
我有以下從服務器值(動態):如何在iPhone上分割字符串?
drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California
現在我想VALU這樣。
drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California
請幫我這個問題
正如其他人所說,你可以使用NSString's
成員函數componentsSeparatedByString:
或componentsSeparatedByCharactersInSet:
作爲替代(更強大的符號化),看看Objective-C的NSScanner類在Mac OS X
的基礎框架你可以做這樣的事情:
NSString *str = "drwxr-xr-x 9 0 ... ";
NSScanner *scanner = [NSScanner scannerWithString:str];
爲了獲得字符串形式每個令牌,使用NSScanner's
scanUpToCharactersFromSet:intoString:
個成員函數。
NSString *token = [NSString string];
NSCharacterSet *div = [NSCharacterSet whitespaceCharacterSet];
[scanner scanUpToCharactersFromSet:div intoString:token];
// token now contains @"drwxr-xr-x"
對上述的後續調用將返回9,0等等。
注意:上面的代碼尚未經過測試。
[myStringValue componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
也可能有用。
使用正則表達式:RegexKitLite。
這是一個使用正則表達式來完成你想要的大量解釋的「完整示例」,所以這是一個很長的答案。所使用的正則表達式只是這樣做的一種方式,對於它所接受的「相當寬鬆」。示例顯示:
Jan 10 05:30
和Apr 30 2009
)NSDictionary
。注:的例子拆分了一些跨多行的長字符串。編譯器將以「自動」方式將@"string1 " @"string2"
形式的字符串文字連接起來,形成一個相當於@"string 1 string2"
的字符串。我只注意到這一點,因爲如果你不習慣這可能看起來有點不尋常。
#import <Foundation/Foundation.h>
#import "RegexKitLite.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *stringToMatch =
@"drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California\n"
@"-rw-r--r-- 1 johne staff 1335 Apr 30 2009 tags.m"; // A random entry from my machine with an "older" date.
NSString *regex =
@"(?m)^" // (?m) means: to "have^and $ match new line boundaries".^means: "Match the start of a line".
// Below,
// (...) means: "Capture for extraction the matched characters". Captures start at 1, capture 0 matches "everything the regex matched".
// [^\\p{Z}]+ says: "Match one or more characters that are NOT 'Separator' characters (as defined by Unicode, essentially white-space)".
// In essence, '[^\\p{Z}]+' matches "One or more non-white space characters."
// \\s+ says: Match one or more white space characters.
// ([^\\p{Z}]+)\\s+ means: Match, and capture, the non-white space characters, then "gobble up" the white-space characters after the match.
@"([^\\p{Z}]+)\\s+" // Capture 1 - Permission
@"([^\\p{Z}]+)\\s+" // Capture 2 - Links (per `man ls`)
@"([^\\p{Z}]+)\\s+" // Capture 3 - User
@"([^\\p{Z}]+)\\s+" // Capture 4 - Group
@"([^\\p{Z}]+)\\s+" // Capture 5 - Size
@"(\\w{1,3}\\s+\\d+\\s+(?:\\d+:\\d+|\\d+))\\s+" // Capture 6 - The "date" part.
// \\w{1,3} means: One to three "word-like" characters (ie, Jan, Sep, etc).
// \\d+ means: Match one or more "digit-like" characters.
// (?:...) means: Group the following, but don't capture the results.
// (?:.A.|.B.) (the '|') means: Match either A, or match B.
// (?:\\d+:\\d+|\\d+) means: Match either '05:30' or '2009'.
@"(.*)$"; // Capture 7 - Name. .* means: "Match zero or more of any character (except newlines). $ means: Match the end of the line.
// Use RegexKitLites -arrayOfCaptureComponentsMatchedByRegex to create an
// "array of arrays" composed of:
// an array of every match of the regex in stringToMatch, and for each match,
// an array of all the captures specified in the regex.
NSArray *allMatchesArray = [stringToMatch arrayOfCaptureComponentsMatchedByRegex:regex];
NSLog(@"allMatchesArray: %@", allMatchesArray);
// Here, we iterate over the "array of array" and create a NSDictionary
// from the results.
for(NSArray *lineArray in allMatchesArray) {
NSDictionary *parsedDictionary =
[NSDictionary dictionaryWithObjectsAndKeys:
[lineArray objectAtIndex:1], @"permission",
[lineArray objectAtIndex:2], @"links",
[lineArray objectAtIndex:3], @"user",
[lineArray objectAtIndex:4], @"group",
[lineArray objectAtIndex:5], @"size",
[lineArray objectAtIndex:6], @"date",
[lineArray objectAtIndex:7], @"name",
NULL];
NSLog(@"parsedDictionary: %@", parsedDictionary);
}
// Here, we use RegexKitLites -stringByReplacingOccurrencesOfRegex method to
// create a new string. We use it to essentially transform the original string
// in to a "comma separated values" version of the string.
// In the withString: argument, '$NUMBER' means: "The characters that were matched
// by capture group NUMBER."
NSString *commaSeparatedString = [stringToMatch stringByReplacingOccurrencesOfRegex:regex withString:@"$1,$2,$3,$4,$5,$6,$7"];
NSLog(@"commaSeparatedString:\n%@", commaSeparatedString);
[pool release];
pool = NULL;
return(0);
}
編譯和運行用:
shell% gcc -Wall -Wmost -arch i386 -g -o regexExample regexExample.m RegexKitLite.m -framework Foundation -licucore
shell% ./regexExample
2010-01-14 00:10:38.868 regexExample[49409:903] allMatchesArray: (
(
"drwxr-xr-x 9 0 0 4096 Jan 10 05:30 California",
"drwxr-xr-x",
9,
0,
0,
4096,
"Jan 10 05:30",
California
),
(
"-rw-r--r-- 1 johne staff 1335 Apr 30 2009 tags.m",
"-rw-r--r--",
1,
johne,
staff,
1335,
"Apr 30 2009",
"tags.m"
)
)
2010-01-14 00:10:38.872 regexExample[49409:903] parsedDictionary: {
date = "Jan 10 05:30";
group = 0;
links = 9;
name = California;
permission = "drwxr-xr-x";
size = 4096;
user = 0;
}
2010-01-14 00:10:38.873 regexExample[49409:903] parsedDictionary: {
date = "Apr 30 2009";
group = staff;
links = 1;
name = "tags.m";
permission = "-rw-r--r--";
size = 1335;
user = johne;
}
2010-01-14 00:10:38.873 regexExample[49409:903] commaSeparatedString:
drwxr-xr-x,9,0,0,4096,Jan 10 05:30,California
-rw-r--r--,1,johne,staff,1335,Apr 30 2009,tags.m
根據您的格式,我會說「值=價值」 ;-) – 2010-01-13 11:48:57
我的意思是,我想下面的格式。 drwxr-xr-x,9,0,0,4096,Jan 10,05:30,加州 – 2010-01-13 11:51:00
添加了代碼標籤。 – 2010-01-13 11:51:32