1

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!!!

2
  • Write your own split function to do it for you. Seems like the fastest/least frustrating approach. Commented Jul 17, 2015 at 17:04
  • NSRegularExpression and NSScanner are the primary framework-provided ways to parsing/breaking up strings. Commented Jul 17, 2015 at 17:20

3 Answers 3

2

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"?

Sign up to request clarification or add additional context in comments.

4 Comments

fast thinking with: [@"-" stringByAppendingString:components[1]]; ... or
Yes, but it was a hint for you to consider other more sophisticated ways to convert a string to a complex number.
Can I use some sort or regular expression or something similar in the condition like @"+|-". Sorry if it is a stupid question but I am quite new in Objective-C
Yes, it is possible but quite cumbersome with Objective-C. Lookup NSRegularExpression if you want. Another option is just iterating over the characters of the string.
1

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;
}

Comments

0

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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.