1

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 a gameserver website, where playernames change...

4
  • 1
    But how do you know they belong together? What are the other names there? Does "TBH" and "DragonFire" belong together as a "TBH Dragonfire". Is it the double space that makes the difference?
    – hol
    Aug 1, 2010 at 14:48
  • i knw they belong together because theyre fetched from a website, so i just checked the website.
    – Pripyat
    Aug 1, 2010 at 15:13
  • I just now saw your answer after I posted my answer. Fetching it from a website means you might have some HTML tags you could interpret or something like this. The are maybe originally separated by some <LI> tag. Check it out.
    – hol
    Aug 1, 2010 at 15:22
  • well it seems to be a table - (online players) gametracker.com/server/82.1.127.18:28960 - at this point i have stripped the tags, but might check the separators out using nsscanner.
    – Pripyat
    Aug 1, 2010 at 15:59

3 Answers 3

4

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.

0
1

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.