我正在尝试按字母顺序重新排列单词。例如,番茄会变得无法控制,或者堆积会变得很难。
我在C语言中找到了一些使用char数组的方法,但是我遇到了在NSString对象范围内工作的问题。
在NSString对象本身中有更简单的方法吗?
答案 0 :(得分:12)
您可以将每个字符串的字符存储到NSArray
个NSNumber
个对象中,然后对其进行排序。看起来有点贵,所以我可能只会使用qsort()
。
这里它是作为Objective-C类别(未经测试)提供的:
的NSString + SortExtension.h:
#import <Foundation/Foundation.h>
@interface NSString (SortExtension)
- (NSString *)sorted;
@end
的NSString + SortExtension.m:
#import "NSString+SortExtension.h"
@implementation NSString (SortExtension)
- (NSString *)sorted
{
// init
NSUInteger length = [self length];
unichar *chars = (unichar *)malloc(sizeof(unichar) * length);
// extract
[self getCharacters:chars range:NSMakeRange(0, length)];
// sort (for western alphabets only)
qsort_b(chars, length, sizeof(unichar), ^(const void *l, const void *r) {
unichar left = *(unichar *)l;
unichar right = *(unichar *)r;
return (int)(left - right);
});
// recreate
NSString *sorted = [NSString stringWithCharacters:chars length:length];
// clean-up
free(chars);
return sorted;
}
@end
答案 1 :(得分:5)
我认为将字符串分隔为字符串数组(数组中的每个字符串只包含原始字符串中的一个字符)。然后排序数组就可以了。这不是很有效,但是当字符串不是很长时就足够了。我已经测试了代码。
NSString *str = @"stack";
NSMutableArray *charArray = [NSMutableArray arrayWithCapacity:str.length];
for (int i=0; i<str.length; ++i) {
NSString *charStr = [str substringWithRange:NSMakeRange(i, 1)];
[charArray addObject:charStr];
}
NSString *sortedStr = [[charArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] componentsJoinedByString:@""];
答案 2 :(得分:2)
// --------- Function To Make an Array from String
NSArray *makeArrayFromString(NSString *my_string) {
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < my_string.length; i ++) {
[array addObject:[NSString stringWithFormat:@"%c", [my_string characterAtIndex:i]]];
}
return array;
}
// --------- Function To Sort Array
NSArray *sortArrayAlphabetically(NSArray *my_array) {
my_array= [my_array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
return my_array;
}
// --------- Function Combine Array To Single String
NSString *combineArrayIntoString(NSArray *my_array) {
NSString * combinedString = [[my_array valueForKey:@"description"] componentsJoinedByString:@""];
return combinedString;
}
// Now you can call the functions as in below where string_to_arrange is your string
NSArray *blowUpArray;
blowUpArray = makeArrayFromString(string_to_arrange);
blowUpArray = sortArrayAlphabetically(blowUpArray);
NSString *arrayToString= combineArrayIntoString(blowUpArray);
NSLog(@"arranged string = %@",arrayToString);
答案 3 :(得分:0)
另一个使用NSMutableString
和sortUsingComparator:
的示例
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"tomat"];
[mutableString appendString:@"o"];
NSLog(@"Orignal string: %@", mutableString);
NSMutableArray *charArray = [NSMutableArray array];
for (int i = 0; i < mutableString.length; ++i) {
[charArray addObject:[NSNumber numberWithChar:[mutableString characterAtIndex:i]]];
}
[charArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 charValue] < [obj2 charValue]) return NSOrderedAscending;
return NSOrderedDescending;
}];
[mutableString setString:@""];
for (int i = 0; i < charArray.count; ++i) {
[mutableString appendFormat:@"%c", [charArray[i] charValue]];
}
NSLog(@"Sorted string: %@", mutableString);
输出:
Orignal string: tomato
Sorted string: amoott