将 Unichar 数组转换为单个 NSString

发布于 2024-12-04 17:36:35 字数 490 浏览 3 评论 0原文

我使用 NSString 对象中的“characterAtIndex:”创建了一个 unicode 字符数组。

我想将数组中的字符再次变成一个完整的字符串。

有什么方法可以将此 unicahr 数组转换为 NSStirng 吗? 有人可以帮我吗???

这是我的代码:

NSString *sentence = @"A long string";
NSString *new = [[NSString alloc]init];
unichar aBuffer[[sent length]];
int j=0;
//Tried to reverse the string:
for (int i=[sent length]-1; i>=0; i--,j++) {
    aBuffer[j]=[sent characterAtIndex:i];
    }

我找到了比这更好的逆转刺痛的方法,但请告诉我我们是否有任何方法可以做到这一点......

I created an unicode char array using 'characterAtIndex:' from an NSString object.

I want to make the characters in the array again into a single complete string.

Is there any method to convert this unicahr Array into NSStirng?
Can anyone please help me???

This s my code:

NSString *sentence = @"A long string";
NSString *new = [[NSString alloc]init];
unichar aBuffer[[sent length]];
int j=0;
//Tried to reverse the string:
for (int i=[sent length]-1; i>=0; i--,j++) {
    aBuffer[j]=[sent characterAtIndex:i];
    }

I found a better way for sting reversal than this, but let me know whether we have any method for this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花似海 2024-12-11 17:36:36

您可以使用免费桥接将 char 数组转换为 Objective-C 类或反向转换。

您可以使用数组创建一个 CFMutableStringRef 变量,假设该变量是 myString。您可以用它创建一个字符串或可变字符串。

NSMutableString* objcString = (NSMutableString*) myString;

You can use Toll-Free Bridging to turn a char array into an objective-c class or reverse.

You can create a CFMutableStringRef variable with your array lets say the variable is myString. Than you can create a string or a mutable string with it.

NSMutableString* objcString = (NSMutableString*) myString;
眼泪都笑了 2024-12-11 17:36:35

您可以使用 getCharacters:range:stringWithCharacters:unichar 相互转换。

NSString *sentence = @"A long unicode sentence";

NSUInteger length = [sentence length];
unichar aBuffer[length + 1];

[sentence getCharacters:aBuffer range:NSMakeRange(0, length)];
aBuffer[length] = 0x0;

NSString *newStr = [NSString stringWithCharacters:aBuffer length:length];

NSLog(@"%@", newStr);

You can convert to a unichar and back using getCharacters:range: and stringWithCharacters:.

NSString *sentence = @"A long unicode sentence";

NSUInteger length = [sentence length];
unichar aBuffer[length + 1];

[sentence getCharacters:aBuffer range:NSMakeRange(0, length)];
aBuffer[length] = 0x0;

NSString *newStr = [NSString stringWithCharacters:aBuffer length:length];

NSLog(@"%@", newStr);
爱的故事 2024-12-11 17:36:35

该数组是标准的 C 数组吗? (看起来像这样:array[50];)如果是,你可以这样做:

[NSString stringWithCharacters:(const unichar*) length:(NSUInteger)];

Is the array a standard c array? (looks something like this: array[50];) If it is, you can do this:

[NSString stringWithCharacters:(const unichar*) length:(NSUInteger)];
用心笑 2024-12-11 17:36:35

使用以下命令从字节数组中获取 NSString:(

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding

您可以使用 UTF8String 而不是使用 characterAtIndex: 循环遍历字符串)

Get a NSString from a bytes array with:

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding

(and you could use UTF8String instead of looping through the string with characterAtIndex:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文