可能重复:
反转 NSString 文本
我是objectovec的新手。我有一个包含字符串的数组。如何反转每个字符串?
哪种方法NSArray
可以NSstring
帮助我?
我想在数组中反转字符串。
谢谢
可能重复:
反转 NSString 文本
我是objectovec的新手。我有一个包含字符串的数组。如何反转每个字符串?
哪种方法NSArray
可以NSstring
帮助我?
我想在数组中反转字符串。
谢谢
创建一个将返回反转字符串的方法。
-(NSString *)reverseString:(NSString *)string{
NSString *reverseString=[NSString new];
for (NSInteger i=string.length-1; i>-1; i--) {
reverseString=[reverseString stringByAppendingFormat:@"%c",[string characterAtIndex:i]];
}
return reverseString;
}
在您的任何方法中:
NSMutableArray *names=[NSMutableArray arrayWithObjects:@"anoop",@"johnson",@"wasim",nil];
for (NSInteger i=0; i<names.count; i++) {
names[i]=[self reverseString:names[i]];
}
NSLog(@"%@",names);
NSString *str=textEntered.text;//
NSMutableArray *temp=[[NSMutableArray alloc] init];
for(int i=0;i<[str length];i++)
{
[temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
}
temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
NSString *reverseString=@"";
for(int i=0;i<[temp count];i++)
{
reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
}
NSLog(@"%@",reverseString);
另一种反转字符串的方法 NSStringEnumerationOptions
- (NSString *)reverseString:(NSString *)string {
NSMutableString *reversedString = [[NSMutableString alloc] init];
NSRange fullRange = [string rangeOfString:string];
NSStringEnumerationOptions enumerationOptions = (NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences);
[string enumerateSubstringsInRange:fullRange options:enumerationOptions usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversedString appendString:substring];
}];
return reversedString;
}
希望这有帮助
NSString *str=textEntered.text;//
NSMutableArray *temp=[[NSMutableArray alloc] init];
for(int i=0;i<[str length];i++)
{
[temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
}
temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
NSString *reverseString=@"";
for(int i=0;i<[temp count];i++)
{
reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
}
NSLog(@"%@",reverseString);
采用
unichar c[yourstring.Length];
NSRange raneg={0,yourstring.Length};
[yourstring getCharacters:c range:raneg];
//c will be an array of your string do what ever you wish
使用这个来反转您的字符串,然后以相同的索引存储回您的数组。
-(NSString*)reverseString:(NSString*)string {
NSMutableString *reversedString;
int length = [string length];
reversedString = [NSMutableString stringWithCapacity:length];
while (length--) {
[reversedString appendFormat:@"%c", [string characterAtIndex:length]];
}
return reversedString;
}
但是如果你有可变字符串,那么你可以创建一个类别
@interface NSMutableString (Reverse)
- (void)reverseString;
@end
@implementation NSMutableString (Reverse)
- (void)reverseString {
for (int i = 0; i < self.length/2; i++) {
int l = self.length - 1 - i;
NSRange iRange = NSMakeRange(i, 1);
NSRange lRange = NSMakeRange(l, 1);
NSString *iStr = [self substringWithRange:iRange];
NSString *lStr = [self substringWithRange:lRange];
[self replaceCharactersInRange:iRange withString:lStr];
[self replaceCharactersInRange:lRange withString:iStr];
}
}
@end
然后你可以像这样使用这个类别方法
NSArray *arr = [NSArray arrayWithObjects:[@"hello" mutableCopy], [@"Do it now" mutableCopy], [@"Test string, 123 123" mutableCopy], nil];
NSLog(@"%@",arr);
[arr makeObjectsPerformSelector:@selector(reverseString)];
NSLog(@"%@",arr);