[英]Can I copy from NSString to NSMutableString?
我有strValue
作為NSString
,我想將strValue
的內容復制到另一個地方。
這個地方是mstrValue
,它是一個NSMutableString
。 已經分配了mstrValue
空間。
我想知道如何為此目的使用memcpy
或strcpy
。
如果不可能,我想知道其他方法。
您的問題不太清楚,但如果您想將可變字符串的值設置為另一個字符串,請執行以下操作:
[mstrValue setString:strValue];
如果要附加值,請執行以下操作:
[mstrValue appendString:strValue];
這兩個假設在某些時候你做了:
mstrValue = [[NSMutableString alloc] init];
看看NSMutableString的文檔。 有很多方法可以更新它的價值。
我總是喜歡使用[NSString stringWithFormat@"%@", strValue];
因為那樣你就可以清楚地得到一個新的自動釋放字符串,並且可以正確處理字符串“strValue”。
NSMutableString *mstrValue = [NSMutableString stringWithFormat:@"%@", strValue];
要么
NSMutableString *mstrValue = [NSMutableString stringWithString:strValue];
除非要正確處理字符串編碼,否則應避免使用memcpy或strcpy。
是,對象分配:
NSMutableString *mstrValue = [[NSMutableString alloc] initWithString:strValue];
你能行的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.