开发者

NSString Question

开发者 https://www.devze.com 2023-01-09 14:17 出处:网络
I\'m trying to extract the names from this list: new String:blood czTheDeAtHTBH DragonFireScotsmanKing KotBobLeeSwaggerAffeMitWaffeJackoboD LfatalityJack bobotelexapaTigerzipGuronzannoobmasterfearpio

I'm trying to extract the names from this list:

new String:  blood cz            TheDeAtH              TBH DragonFire   Scotsman          King Kot            BobLeeSwagger          AffeMitWaffe    Jackobo          D L  fatality         Jack bobo           telex             apa           Tiger            zip     Guronzan          noobmaster           fear           piotrekjankiewi         RoCJackal 

These names will then have to be written into an array. I've done this so far:

NSString *newString = [[test componentsSeparatedByCharactersInSet:
       [[NSCharacterSet letterCharacterSet] invertedSet]] 
         componentsJoinedByString:@";"]; //yes there are spaces in the above list, previously they were replaced by ;

I then load it into an array:

 NSArray *chunks = [newString componentsSeparatedByString:@";"];

This works, but for the name "blood cz" for example, it takes blood and cz as a seperate thing in the array, but i need them to be together.

Does anyone have an idea of how I could achieve this? I've been breaking my head over this, because Im not able to seperate the strings. When taking the whitespaces out, its all together and I can seperate the strings, reason being that these strings are being taken from开发者_JAVA技巧 a gameserver website, where playernames change...


This worked

NSString         *mystring = @"blood cz            TheDeAtH              TBH DragonFire   Scotsman          King Kot            BobLeeSwagger          AffeMitWaffe    Jackobo          D L  fatality         Jack bobo           telex             apa           Tiger            zip     Guronzan          noobmaster           fear           piotrekjankiewi         RoCJackal";
NSString *mystring2 = [mystring stringByReplacingOccurrencesOfString:@"  "  withString:@";"];
    NSCharacterSet   *separator   = [NSCharacterSet     characterSetWithCharactersInString:@";"];
NSArray          *chunks      = [mystring2 componentsSeparatedByCharactersInSet:separator];

    for(NSString * myStr in chunks) {
        NSLog(myStr);
    }

You still have to left trim the names (cutting of leading spaces) but I left some work for you.

Also I want to add that this is best called a "hack". Because it will NOT WORK anymore if a user enters a double space in a name.

So I would take a step back and ask myself why I get such a lousy string to interpret and try to solve the problem at the source of the problem ... This is my core answer: Check out whether you could get a better string.


You can't just replace the whitespaces with ; only if there are two whitespaces?


I am not sure but this one looks for me like a tab \t rather than a space. Another solution is like Jonas's one. You can try to separate 2 strings only when there are more than 2 spaces between them

0

精彩评论

暂无评论...
验证码 换一张
取 消