depecheSoul
depecheSoul

Reputation: 956

Splitting string in Objective-C without removing separating string

I am trying to split a string that is a complex number into real and imaginary number.

I tried to find the solution on the internet, but all of the solutions that I found remove the splitting character.

I will show on example what exactly I want to do:

I have a string that is in this form : -3.5+6.7i

I want to split the string into -3.5 and +6.7i

Thanks for the help!!!

Upvotes: 1

Views: 183

Answers (3)

Ewan Mellor
Ewan Mellor

Reputation: 6847

The accepted answer is terrible, it doesn't handle any of the obvious corner cases. Try this:

NSString * input = @"-3.5+6.7i";

NSString * const floatRe = @"\\d*(?:\\.\\d*)?";
NSString * const reStr = [NSString stringWithFormat:@"([-+]?%@)([-+]%@)i", floatRe, floatRe];
NSRegularExpression * re = [NSRegularExpression regularExpressionWithPattern:reStr options:(NSRegularExpressionOptions)0 error:NULL];
NSArray * matches = [re matchesInString:input options:(NSMatchingOptions)0 range:NSMakeRange(0, input.length)];
if (matches.count != 1) {
   // Fail.
}
NSTextCheckingResult * match = matches[0];
double real = [[input substringWithRange:[match rangeAtIndex:1]] doubleValue];
double imag = [[input substringWithRange:[match rangeAtIndex:2]] doubleValue];

NSLog(@"%lf / %lf", real, imag);

Upvotes: 0

Adam Evans
Adam Evans

Reputation: 2082

Try this function. Haven't tested it, so it may need some tweaking

+ (NSMutableArray*)split:(NSString*)string on:(NSArray*)separators
{
    NSMutableArray* answer = [[NSMutableArray alloc] init];
    NSString* substring = [NSString stringWithString:string];

    //slowly shrink the string by taking off strings from the front
    while ([substring length] > 0)
    {
        int first = 0;

        //look for the separator that occurs earliest and use that for what you are
        //splitting on. There is a slight catch here. If you have separators "abc" and "bc",
        //and are looking at string "xabcd", then you will find the strings "x", "a", and
        //"bcd" since the separators share common substrings, meaning that the strings
        //returned from this function are not guaranteed to start with one of the
        //separators.
        for (int j = 0; j < [separators count]; j++)
        {
            //need to start from index 1 so that the substring found before that caused
            //the split is not found again at index 0
            NSString* toCheck = [substring substringFromIndex:1];
            int start = [substring rangeOfString:[separators objectAtIndex:j]].location;

            if (start < first)
            {
                first = start;
            }
        }

        [answer addObject:[substring substringToIndex:start]];
        substring = [substring substringFromIndex:start];
    }

    return answer;
}

Upvotes: 1

Glorfindel
Glorfindel

Reputation: 22641

This is quite easy:

NSString *complexNumber = @"-3.5+6.7i";
NSArray *components = [complexNumber componentsSeparatedByString:@"+"];
NSString *realPart = components[0];
NSString *imaginaryPart = [@"+" stringByAppendingString:components[1]];

Next question: how are you going to split @"-3.5-6.7i"?

Upvotes: 2

Related Questions