如何在C#中将int转换为char时删除空格? [英] How to remove the white space while converting int to char in C# ?

查看:84
本文介绍了如何在C#中将int转换为char时删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我想转换成char而我试过这段代码

var floor = Convert.ToChar(ddlFloor .SelectedIndex);



但是这里我转换为char后没有得到正确的值,例如如果ddlFloor.SelectedIndex是2那么我得到的值是楼层是 2'''



这意味着楼层= 2'''为什么单引号在数字后面?



当我尝试将其插入表中时(在表格中我有一个带有char数据类型的Floor列),它将插入空值。为什么?



提前致谢



Abdul Salam

Hi Friends,

I want to convert into to char and I tried this code
var floor=Convert.ToChar(ddlFloor.SelectedIndex);

but here I am not getting the correct value after converting to the char,for example if ddlFloor.SelectedIndex is 2 then I am getting the value for floor is 2''''

that means floor=2'''' why single quotes coming after number?

and when I try to insert it into the table (in table I have Floor column with char data type) it will insert null value. Why?

Thanks in advance

Abdul Salam

推荐答案

我认为你需要 ddFloor.SelectedIndex.ToString 。并且不要忘记在没有选择的情况下考虑这个案例。



然而,我不确定整个想法是否正确。为什么要获取所选索引的字符串表示?我在问,因为,现在,使用字符串表示数据的趋势是许多人真正的病态。只有当您想要在屏幕上显示某些内容时,才需要字符串表示,有时需要存储在文件或流中。没有人需要执行计算转换(不是真正正确的术语)到字符串和返回,但许多人做...不要这样做。



- SA
I think you rather need ddFloor.SelectedIndex.ToString. And don''t forget to take into account the case when nothing is be selected.

However, I''m not sure the whole idea is right. Why getting string representation of selected index at all? I''m asking, because, these days, a trend to work with string representation of data is a real sickness of many. String representation is only needed when you want to show something on screen, sometimes to store in a file or a stream. Nobody needs to perform calculations "converting" (not really correct term) to strings and back, but many do… Don''t do it.

—SA


我假设您正在调试器中查看 floor 的值。 />
显示 2''''是因为Unicode值为2的字符没有可显示的表示形式。

调试器始终显示两者字符的数字值和撇号字符之间的字符本身。

我认为你要完成的是获取数值的字符表示,在这种情况下你应该使用 Convert.ToString()
I''m assuming you''re looking at the value of floor in the debugger.
The display of 2 '' '' is because the character with Unicode value of 2 does not have a displayable representation.
The debugger always shows both the numeric value of the character and the character itself between the apostrophe characters.
I think what you are trying to accomplish is to get the character representation of the numeric value, in which case you should use Convert.ToString().


会员9233080问:
Member 9233080 asked:

你好谢尔盖,你能解释抽象类和界面与现实世界的例子之间的区别吗?提前谢谢。

Hello Sergey, could you explain the difference between abstract class and interface with real world example? Thanks in advance.

好吧,关于差异的问题并不完全正确。你能否定义什么是差异? (Apple和Apple有什么区别?:-))



尽管如此,考虑使用抽象类和接口很重要,因为在现实生活中,决定使用接口可以被视为使用抽象类的替代方法,或者两种方法可以组合使用。做出决定并非易事,也不容易。并且简单地解释并不容易;尽管许多人试图简单地解释它,但这些解释很少反映事物的本质,它们很少为良好的代码设计提供有效的实用标准。



让我试试。请看这些过去的讨论,包括我的答案:

当我们使用抽象时,当我们使用接口......? [ ^ ],

抽象类和接口之间的差异,如果它们具有相同的方法和变量 [ ^ ],

如何决定选择抽象类或接口 [ ^ ],

接口和多态性 [ ^ ]。



至于例子,简短这些用法的例子很容易编写,但它们只能显示语法,你可以自己轻松找到,但是一个简短的例子几乎无助于做出正确的选择。如果需要,也许你会更好地问你后续问题。



-SA

Well, the question about "difference" is not quite correct. Could you possibly define what is "difference"? (What is the difference between apple and Apple? :-))

Nevertheless, considering the use of abstract classes vs interfaces is important, because in real life, the decision of using interfaces can be considered as the alternative to using abstract classes, or both approaches could be combined. Making the decision is not trivial and not easy. And it''s not so easy to explain in brief; even though many tried to explain it simply, such explanations rarely reflected the essence of things and they rarely provided valid practical criteria for good code design.

Let me try. Please see these past discussions, including my answers:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

As to the examples, the short examples of the usage are easy to write, but they can only show the syntax, which you can easily find out by yourself, but a short examples will hardly help to make a right choice. Maybe you will better ask you follow-up questions if you need.

—SA


这篇关于如何在C#中将int转换为char时删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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