如何在特定字符串后将字符串放入字符串

时间:2022-09-13 09:27:18

I am new in programming. I have string NSString *string = @"\U0420\U043e\U0437\U044b"; and after each slash('\') i need put another slash to get string like this @"\\U0420\\U043e\\U0437\\U044b"

我是编程新手。我有字符串NSString * string = @“\ U0420 \ U043e \ U0437 \ U044b”;在每个斜杠('\')之后我需要另外一个斜杠来获取这样的字符串@“\\ U0420 \\ U043e \\ U0437 \\ U044b”

I am new to programming and objective-c. please help.

我是编程和目标的新手。请帮忙。

1 个解决方案

#1


3  

My original answer was:

我原来的答案是:

Use [NSString stringByReplacingOccurrencesOfString:withString:] (reference).

使用[NSString stringByReplacingOccurrencesOfString:withString:](引用)。

NSString *string = @"\U0420\U043e\U0437\U044b";
NSString *converted = [string stringByReplacingOccurrencesOfString:@"\\" 
                                                        withString:@"\\\\\\"];

However I now don't think that's right given the \ characters won't actually exist in string; instead the compiler will convert each of those sequences into a unicode character. You will need to encode string as this:

但是我现在不认为这是正确的\字符实际上不会存在于字符串中;相反,编译器会将每个序列转换为unicode字符。您需要将字符串编码为:

NSString *string = @"\\U0420\\U043e\\U0437\\U044b";

In order to use the above code. I cannot see any alternative to this.

为了使用上面的代码。我看不到任何替代方案。

Further Update: Often when I've come across questions like this there is a confusion between string literals and string data. In your question those \ characters won't appear as the compiler will have converted them into unicode characters (\Uxxx is a unicode escape sequence for a single character). However if you provided a string like that at runtime (say read from a text file) then those \ characters will exist and you can use the code above.

进一步更新:通常当我遇到这样的问题时,字符串文字和字符串数据之间存在混淆。在你的问题中,这些\字符不会出现,因为编译器会将它们转换为unicode字符(\ Uxxx是单个字符的unicode转义序列)。但是,如果您在运行时提供了类似的字符串(例如从文本文件中读取),则这些\字符将存在,您可以使用上面的代码。

#1


3  

My original answer was:

我原来的答案是:

Use [NSString stringByReplacingOccurrencesOfString:withString:] (reference).

使用[NSString stringByReplacingOccurrencesOfString:withString:](引用)。

NSString *string = @"\U0420\U043e\U0437\U044b";
NSString *converted = [string stringByReplacingOccurrencesOfString:@"\\" 
                                                        withString:@"\\\\\\"];

However I now don't think that's right given the \ characters won't actually exist in string; instead the compiler will convert each of those sequences into a unicode character. You will need to encode string as this:

但是我现在不认为这是正确的\字符实际上不会存在于字符串中;相反,编译器会将每个序列转换为unicode字符。您需要将字符串编码为:

NSString *string = @"\\U0420\\U043e\\U0437\\U044b";

In order to use the above code. I cannot see any alternative to this.

为了使用上面的代码。我看不到任何替代方案。

Further Update: Often when I've come across questions like this there is a confusion between string literals and string data. In your question those \ characters won't appear as the compiler will have converted them into unicode characters (\Uxxx is a unicode escape sequence for a single character). However if you provided a string like that at runtime (say read from a text file) then those \ characters will exist and you can use the code above.

进一步更新:通常当我遇到这样的问题时,字符串文字和字符串数据之间存在混淆。在你的问题中,这些\字符不会出现,因为编译器会将它们转换为unicode字符(\ Uxxx是单个字符的unicode转义序列)。但是,如果您在运行时提供了类似的字符串(例如从文本文件中读取),则这些\字符将存在,您可以使用上面的代码。