Working with a string as an array of characters
Posted
by
Malfunction
on Stack Overflow
See other posts from Stack Overflow
or by Malfunction
Published on 2013-06-30T11:09:14Z
Indexed on
2013/06/30
22:21 UTC
Read the original article
Hit count: 135
objective-c
I'm having some trouble with a string represented as an array of characters. What I'd like to do, as I would do in java, is the following:
while (i < chars.length) {
char ch = chars[i];
if ((WORD_CHARS.indexOf(ch) >= 0) == punctuation) {
String token = buffer.toString();
if (token.length() > 0) {
parts.add(token);
}
buffer = new StringBuffer();
}
buffer.append(ch);
i++;
}
What I'm doing is something like this:
while(i < strlen(chars)) {
char ch = chars[i];
if(([WORD_CHARS rangeOfString:ch] >= 0) == punctuation) {
NSString *token = buffer.toString();
if([token length] > 0) {
[parts addObject:token];
}
buffer = [NSMutableString string];
}
[buffer append(ch)];
i++;
}
I'm not sure how I'm supposed to convert
String token = buffer.toString();
to objective c, where buffer is an NSMutableString. Also, how do I check this if condition in objective c?
if ((WORD_CHARS.indexOf(ch) >= 0) == punctuation)
WORD_CHARS is an NSString. I'm also having trouble with appending ch to buffer.
Any help is greatly appreciated.
© Stack Overflow or respective owner