NSString内容因为字符串中间的空字符而被截断 [英] NSString contents get truncated because of the null character in the middle of the string

查看:274
本文介绍了NSString内容因为字符串中间的空字符而被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在作为HTTP Post响应的一部分获得的字符串的中间获得空字符。因此,内容只打印到空字符,即使字符串中有更多的数据。下面是示例问题的示例代码

I am getting null character in the middle of the string obtained as a part of response to HTTP Post. As a result the content gets printed only upto the null character even though the string has more data in it. Below is the sample code that illustrates the problem

 NSString *testString= @"v1db1���������¿¿sssss"; ->this line shows the null character warning
 NSInteger stringlength = [testString length];
 NSLog(@"String Length:%ld",stringlength);
 NSLog(@"String Value:%@",testString);

注意:测试字符串包含带有问号的空字符。因为某些原因,如果我复制确切的字符串,我无法保存帖子。

Note: the test string contains a null character with the question mark. For some reason I am not able to save the post if I copy the exact string.

第一行显示警告字符串文字中保留的空字符

The first line shows a warning "Null character(s) preserved in string literal" in Xcode.

这个程序的输出是

 String Length:21
 String Value:v1db1

解决这个问题的正确方法是什么?我想考虑扫描NSString任何空字符和删除相同。什么可能是我得到空字符的原因?

What is the correct approach to solve this problem?. I am thinking of scanning the NSString for any null character and removing the same. What could be the reason I am getting null character ?.

推荐答案

问题字符是UTF-8编码的东西(EF BF BD和C2 BF),直接在引号字符串在XCode。你需要转换它。类似:

The problem characters are UTF-8 encoded something (EF BF BD and C2 BF) that are handled badly directly in quoted strings in XCode. You will need to convert it. Something like:

[ NSString stringWithUTF8String: [ @"v1db1���������¿¿sssss" cStringUsingEncoding: [ NSString defaultCStringEncoding ] ] ];

这篇关于NSString内容因为字符串中间的空字符而被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆