开发者

How to divide a string to a charecter wise?

开发者 https://www.devze.com 2023-03-17 17:03 出处:网络
I am having a string @\"sairam\". I need a characters separately and i need to st开发者_开发百科ore it into an ARRAY like

I am having a string @"sairam". I need a characters separately and i need to st开发者_开发百科ore it into an ARRAY like

array = [@"s",@"a",@"i",@"r",@"a",@"m"];

how to do this give a loop like if i give a string it needs to divide charecters and store it into an array ...

thanks in advance...


From http://www.idev101.com/code/Objective-C/Strings/split.html:

  NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
  for (int i=0; i < [myString length]; i++) {
      NSString *ichar  = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
      [characters addObject:ichar];
  }


It can be done in following way: Steps : 1. First convert the NSString to cString using cStringUsingEncoding. 2. Then iterate the cString and convert back each char to NSString if you require.


    for (int i=0; i<[passonString length]; i++) 
    {

            unichar tempString=[passon_phoneNumber characterAtIndex:i];
           [array addObject:[NSString stringWithFormat:@"%c",tempString];
}


unichar c[string.length];
    char b;
    NSRange range={0,string.length};
    [<string> getCharacters:c range:range];
    printf("%c%c",c[0],c[1]);

i used this myself as it gave me a manageable character array you can see this link as well you can creatively use this character array as u wish

0

精彩评论

暂无评论...
验证码 换一张
取 消