NSString padding / space between characters
Asked Answered
S

3

6

I'm looking for an easy way in Obj.C to do add a space between each character of my string. So "1234" would come out looking like "1 2 3 4".

I've found a perfect javascript example here: https://mcmap.net/q/423267/-add-a-space-between-characters-in-a-string-duplicate

Does anyone know of something similar for Obj.C? Kerning is a PITA in iOS, and this is ultimately all I need anyway...

Thoughts / comments?

Thanks! - Drew

Selfrealization answered 16/6, 2012 at 3:37 Comment(1)
You can even just transcribe the javascript into Obj-C (this is basic!). What have you tried?Vulcanite
A
5

Try This :

NSString *string =[NSString stringWithString:@"1234"];
NSMutableArray *buffer = [NSMutableArray arrayWithCapacity:[string length]];
for (int i = 0; i < [string length]; i++) {
    [buffer addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
}
NSString *final_string = [buffer componentsJoinedByString:@" "];
Aenea answered 16/6, 2012 at 3:55 Comment(4)
That looks like it works. Just a thought; I believe you could also put this line - [buffer addObject:[NSString stringWithFormat:@" "] - in the for loop after you add the object from the string string.Aten
@qegal ya we can use that but buffer is NSMutableArray so in the end we need to use componentsJoinedByString because we want to get NSStringAenea
This will break for characters that are more than one index (like many chinese characters). It may also break for characters like é which is built up of ´ and e. You could end up with "idé" turning into "i d ´ e"Underpinning
Hector, I don't believe that I gave the down vote, did I? I hope not, what you did worked out great. Thanks for the tip David. Ultimately, this will only be a string of numbers that I'll be using this on and not with any real letters or words.Selfrealization
E
9

To do this correctly, taking into account the problems mentioned in David Rönnqvist's comment, do something like this:

NSMutableString* result = [origString mutableCopy];
[result enumerateSubstringsInRange:NSMakeRange(0, [result length])
                           options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationSubstringNotRequired
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
     if (substringRange.location > 0)
         [result insertString:@" " atIndex:substringRange.location];
}];
Earthnut answered 16/6, 2012 at 10:49 Comment(0)
A
5

Try This :

NSString *string =[NSString stringWithString:@"1234"];
NSMutableArray *buffer = [NSMutableArray arrayWithCapacity:[string length]];
for (int i = 0; i < [string length]; i++) {
    [buffer addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]];
}
NSString *final_string = [buffer componentsJoinedByString:@" "];
Aenea answered 16/6, 2012 at 3:55 Comment(4)
That looks like it works. Just a thought; I believe you could also put this line - [buffer addObject:[NSString stringWithFormat:@" "] - in the for loop after you add the object from the string string.Aten
@qegal ya we can use that but buffer is NSMutableArray so in the end we need to use componentsJoinedByString because we want to get NSStringAenea
This will break for characters that are more than one index (like many chinese characters). It may also break for characters like é which is built up of ´ and e. You could end up with "idé" turning into "i d ´ e"Underpinning
Hector, I don't believe that I gave the down vote, did I? I hope not, what you did worked out great. Thanks for the tip David. Ultimately, this will only be a string of numbers that I'll be using this on and not with any real letters or words.Selfrealization
P
0

Do this:

    NSString *string =[NSString stringWithString:@"1234"];

    NSMutableString *spacedString= [NSMutableString stringWithString:[NSString stringWithFormat:@"%C",[string characterAtIndex:0]]];

    for(int i = 1; i<[string length];i++)
    {
        [spacedString appendString:[NSString stringWithFormat:@" %C",[string characterAtIndex:i]]];
    }
Panteutonism answered 16/6, 2012 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.