English 中文(简体)
将文字写成文字、数字和图象
原标题:Split a text into words, digits and punctuation marks

我需要把一句话分为几句、数字、图象和空间/表格。 我也想维护事情的次序。

NSString *text = [NSString stringWithFormat:@"The 3 quick:"brown fox, jump s" over."];

这是我需要编制的名单:

[ The ,    ,  3 ,    ,  quick,  : ,  " ,  brown ,    ,  fox ,  , ,    ,  jump s ,    ,  . ]

谢谢!

最佳回答

介绍这一类,我用NSScanner &NSCharacterSet写道:

@interface NSString(Splitting)

-(NSArray *) arrayBySeparatingComponentsInCharacterSet:(NSCharacterSet *) charSet;

@end

@implementation NSString(Splitting)

BOOL scanOneCharacterFromSetIntoString(NSScanner *self, NSCharacterSet * charSet, NSString **outStr);
BOOL scanOneCharacterFromSetIntoString(NSScanner *self, NSCharacterSet * charSet, NSString **outStr)
{
    // check for index out of bounds
    NSString *inStr = self.string;

    if (self.scanLocation >= inStr.length)
    {
        return NO;
    }

    unichar ch = [inStr characterAtIndex:self.scanLocation];

    if (![charSet characterIsMember:ch])
    {
        return NO;
    }

    self.scanLocation++;
    if (outStr)
    {
        *outStr = [NSString stringWithCharacters:&ch length:1];
    }

    return YES;
}

-(NSArray *) arrayBySeparatingComponentsInCharacterSet:(NSCharacterSet *)charSet
{
    NSScanner *scanner = [NSScanner scannerWithString:self];
    NSMutableArray *result = [NSMutableArray array];

    NSString *temp = nil;
    while ([scanner scanUpToCharactersFromSet:charSet intoString:&temp] || scanOneCharacterFromSetIntoString(scanner, charSet, &temp)) {;
        [result addObject:temp];

        if ([scanner scanLocation] >= [self length])
        {
            break;
        }

        unichar temp2 = [self characterAtIndex:[scanner scanLocation]];

        if ([charSet characterIsMember:temp2])
        {
            [result addObject:[NSString stringWithFormat:@"%c", temp2]];
            // only update the scan location if the scan was sucessful
            [scanner setScanLocation:[scanner scanLocation] + 1];
        }
    }

    return result;
}

@end

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSString *str = @"The 3 quick:"brown fox, jump s" over.";
        NSArray *array = [str arrayBySeparatingComponentsInCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@" :", ."]];
        NSLog(@"%@", array);
    }
}

你们应该做些什么,把《特征集》改变到你们需要的东西。 还指出,与评价中心一起汇编了这一数据,因此,它可能或可能无法在计算参照的环境下妥善管理记忆。

问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签