簡體   English   中英

如何將char分配給字符串?

[英]how to assign char to string?

我有一個string Lat ,其中包含單詞,我想將它的最后一個字母存儲在新string ,但這給了我一個錯誤: "cannot implicitly convert "char" to string"

current =Lat[j];

currentstring ,當我嘗試使用#.ToString ,它給了我另一個錯誤: Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?

最簡單的方法是

current = Lat[j].ToString();

要么

current = Lat.Substring(Lat.Length - 1);

但是通過索引器訪問字符串有點奇怪-如果您發布更多代碼,可能會有更簡潔的方式來獲取所需的信息。

使用#.ToString()方法將其轉換為字符串。

像這樣: current = Lat[j].ToString()

current =Lat[j].ToString();

或無論如何都將current聲明為char(不能從您的用法中分辨出這是否是個好主意)

如果要在字符串中使用最后一個字符,請使用System.Linq檢索最后一個字母。

string newString = original.Last().ToString();
// original.Last() gives you a char
// .ToString function after changes it to a string.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM