- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用以下代码将字符串数据存储在 char* 中。
NSString *hotelName = [components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
hotelInfo->hotelName = malloc(sizeof(char) * hotelName.length + 1);
strncpy(hotelInfo->hotelName, [hotelName UTF8String], hotelName.length + 1);
NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);
问题出在打印奇怪的希腊字符上。我还尝试使用另一种编码(例如 NSWindowsCP1253StringEncoding -it crashes- )
我什至尝试过:
hotelInfo->hotelName = (const char *)[hotelName cStringUsingEncoding:NSUnicodeStringEncoding];
但它也会产生奇怪的字符。
我错过了什么?
编辑:经过一些建议后,我尝试了以下方法:
if ([hotelName canBeConvertedToEncoding:NSWindowsCP1253StringEncoding]){
const char *cHotelName = (const char *)[hotelName cStringUsingEncoding:NSWindowsCP1253StringEncoding];
int bufSize = strlen(cHotelName) + 1;
if (bufSize >0 ){
hotelInfo->hotelName = malloc(sizeof(char) * bufSize);
strncpy(hotelInfo->hotelName, [hotelName UTF8String], bufSize);
NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);
}
}else{
NSLog(@"String cannot be encoded! Sorry! %@",hotelName);
for (NSInteger charIdx=0; charIdx<hotelName.length; charIdx++){
// Do something with character at index charIdx, for example:
char x[hotelName.length];
NSLog(@"%C", [hotelName characterAtIndex:charIdx]);
x[charIdx] = [hotelName characterAtIndex:charIdx];
NSLog(@"%s", x);
if (charIdx == hotelName.length - 1)
hotelInfo->hotelName = x;
}
NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);
}
但还是没有!
最佳答案
首先,不保证任何 NSString
都可以表示为 C 字符数组(所谓的 C-String)。原因是可用的字符集有限。您应该检查字符串是否可以转换(通过调用 canBeConvertedToEncoding:
)。
其次,当使用malloc
和strncpy
函数时,它们依赖于C-String的长度,而不是C-String的长度NSString
。因此,您应该首先从 NSString 获取 C 字符串,然后获取它的长度 (strlen
),并将该值用于函数调用:
const char *cHotelName = (const char *)[hotelName cStringUsingEncoding:NSWindowsCP1253StringEncoding];
int bufSize = strlen(cHotelName) + 1;
hotelInfo->hotelName = malloc(sizeof(char) * bufSize);
strncpy(hotelInfo->hotelName, cHotelName, bufSize);
关于ios - NSString 到带有希腊字符的 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42361800/
使用 WSDL2ObjC 我得到了很多类,它们是 NSString 的子类。我发现初始化这些类的任何对象的 NSString 值很麻烦。 假设我的类(class)是这样的: @interface mC
当调用 +[NSURL URLWithString:] 时,我有两个构建 URL 的选项: [[@"http://example.com" stringByAppendingPathComponent
我需要删除字符串末尾的空格。我怎样才能做到这一点?示例:如果字符串是 "Hello " 它必须变成 "Hello" 最佳答案 摘自这里的答案:https://stackoverflow.com/a/5
NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 如何将此字符串转换为字节? 最佳答案 NSData *bytes = [test dataUsingEn
我对 NSString 变量有疑问。 .h 文件 NSString *strDeleteFilePath; @property (nonatomic,retain) NSString*
我对这个功能有疑问: - (instancetype)initWithCenter:(CLLocationCoordinate2D)center
如果我有方法 - (void) myMethod:(NSString *)string { [Object anothermethodWithString:string]; } 我打电话 [O
ios 中初始化字符串变量的两种方式有什么区别(,adv/disadv)? NSString *var = @"value" 和 NSString *var =[ [NSString alloc] i
我认为这个问题已经足够清楚了,但仍然 - 有什么区别: NSString *string = @"Hello world!"; 和 NSString *string = [[NSString allo
我正在尝试将 NSString 转换为单个字符。当我写 [[NSLocale] currentLocale] objectForKey:NSLocaleDecimalSeparator] 时,我得到
我有指示制作一个前缀方法,该方法在每个位置采用两个字符串,其中 mask = 0 并且第一个字符串 = 第二个字符串,直到不满足这些条件为止,即您的前缀 NSString。 我做了尝试,但由于某种原因
我有 3 个 NSString 和 1 个带值的 NSArray,我尝试将所有字符串转换为一个: string01 = [string01 stringByAppendingString:[numbe
我有一个包含以下文本的 NSString: You can now download "file.png" from "http://www.example.com/RANDOM.png" 如何使用该
我有一个完整的 URL,比方说 http://www.mywebsite.com//Folder /Detals /Final Image /La Image Logo.jpg 在这个NSString
在某些情况下,我可能有一个包含多个单词的 NSString,其中一些是重复的。我想要做的是采用如下所示的字符串: One Two Three Three Three Two Two Two One O
所以我有一个 NSString,让我们说:NSString *mainString = @"My Name is Andrew (I like computers)";我想从 mainString 中
这是我的职责。首先 println 将正确的散列打印到控制台,但在下一行程序崩溃。你能帮助我吗? func sha256(string: NSString) -> NSString { var
我刚刚看到一行由同事编写的代码,我以前从未在 Obj-C 中见过它。它看起来形式很糟糕,我想更改它,但它似乎有效(应用程序按预期工作),我什至不知道将其更改为什么。 NSString **string
我花了很长时间寻找一种获取 nsstring 对象的匹配计数的方法。但我找不到。如何获取 String_one 和 String_Two 的匹配计数?我需要你的帮助.. NSString *Strin
在我看来,我正在处理的代码应该是这样写的: if (!instructions) { instructions = [[UITextView alloc] init]; ins
我是一名优秀的程序员,十分优秀!