如何连接“字符串”并且“Int”进入Int类型变量 [英] How To Concatenate The "String" And "Int" Into Int Type Variable

查看:86
本文介绍了如何连接“字符串”并且“Int”进入Int类型变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  concatenate   stringint into   int   type   variable  


e g

int a = 10;
string b =abc
a = a + b;



可能???
如何???

解决方案

  int  a =  10 ; 
string b = abc
a = a + b; // 不可能,因为整数不能包含字符,只允许数字(degits)
b = b + a.ToString(); // 可能因为字符串可以包含数字,字符和特殊字符;

< br $>
快乐编码!

:)




一些基本的东西,



假设我给你10个苹果和5个橙子,并问你有多少苹果,你回复15个苹果。这对你有意义吗?



int是整数的缩写,用英语表示数字,即0,1,2,3,..... br />
string是一个char数组(字符的缩写)''H''''''''''''''''''''' $ b

你可以将int转换成字符串,电脑神奇地为你获取相应的字符。但你不能将字符串转换为int,除非该字符串对应一个数字。



例如:

  int  a =  10 ; 

string aToString = a.ToString(); // 是的,这是可能的,因为字符串可以取任何char表示的值。

string b = B;

int bToInt = int .Parse(b);
int bToInt = Convert.ToInt32(b);





以上代码都不会起作用,因为b不是数字的表示。



但是,如果b的值是数字的表示,那么上面的代码会将字符串转换为int。



在你的解决方案2中,你通过分配一个对象使情况变得更糟。对象是一种类型,它不知道您指定的值是一个数字还是一个字符,它就像一个篮子,你不知道它是苹果还是橙子。如果你碰巧在篮子里有橙子,并要求将篮子的内容转换为苹果,这是一项不可能完成的任务。这同样适用于你的解决方案2,它会抛出异常。





我希望你能理解这一点。



问候

Jegan


  int  num =  1000 ; 
string texto = Texto;
string resultado = string .Concat(num.ToString(),texto);


concatenate the "string" and "int" into int type variable


e.g

int a=10;
string b="abc"
a = a+ b;



is it possible???
how???

解决方案

int a=10;
string b="abc"
a = a + b; // NOT POSSIBLE  because integer can not contain character it allows only numbers(degits)
b = b + a.ToString(); // POSSIBLE  because string can contain numbers, chars and special chars;


Happy Coding!
:)


Hi,
Some basic stuff,

Let say that I give you 10 apples and 5 oranges and ask you how many apples you have, and you reply 15 apples. Does that make any sense to you?

int is short for integers, in English it means numbers i.e. 0,1,2,3,.....
string is an array of char ( short for Characters) ''H'' ''e'' ''l'' ''l'' ''o''.

you can convert an int to string, the computer magically get the corresponding character for you. But you cannot convert a string to an int unless that string corresponds to a number.

For example:

int a = 10;

string aToString = a.ToString();  // yes this is possible because string can take any char represented value.

string b = "B";

int bToInt = int.Parse(b);
int bToInt = Convert.ToInt32(b);



Neither of the above code will work, because b is not a representation of number.

However, if the value of b is a representation of a number, then the above codes will convert the string to int.

In your solution 2 you making it even worse by assigning to an object. An object is a type that doesn''t know whether the value you assigned is a number or character, its like a basket that you do not know whether it has apples or oranges. If you happen to have oranges in the basket and ask to convert the contents of the basket to apples, it is an impossible task. the same applies to your solution 2, it will throw exception.


I hope you understand this.

Regards
Jegan


int num = 1000;
string texto = "Texto";
string resultado = string.Concat(num.ToString(), texto);


这篇关于如何连接“字符串”并且“Int”进入Int类型变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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