如何从字符串中获取整数中的逗号
本文关键字:整数 获取 字符串 | 更新日期: 2023-09-27 18:28:48
我有两个整数值,例如25
和3
。现在我想把它们加成一个整数,但条件是这两个值都应该用逗号(,)分隔。如果可能的话,怎么办。这给了我一个错误,因为逗号不能用整数表示。。我已经试过了。。
int inoutSpecifierPosition = (startIndex + "," + difference);
但它给了我错误。。请帮帮我。。。
如有任何建议,我们将不胜感激。。。
不可能有像25,3
这样的整数。它可以是string
,而不是类似;
string inoutSpecifierPosition = startIndex + "," + difference;
任何整数都不能有逗号、十进制分隔符或千位分隔符。它们只是数字。只有它们的字符串表示形式才能具有。这就是为什么你的
现在我想把这些值加成一个整数,但条件是这两个值都应该用逗号分隔
这句话毫无意义。
您的代码出现错误,因为string + int
返回的是string
,而不是int
。
NET框架中的字符串串联中有3个+
运算符重载。
来自C#规范$7.8.4加法运算符
string operator + (string x, string y);
string operator + (string x, object y);
string operator + (object x, string y);
binary+运算符的这些重载执行字符串串联。如果字符串串联的操作数为null,则空字符串为被取代否则,任何非字符串参数都将转换为通过调用虚拟ToString方法的字符串表示继承自类型对象。
如果您想将整数格式化为字符串,可以使用类似string.Format
的格式;
string s = string.Format("{0},{1}", startIndex, difference); // 25,3
如果已经将25,3
作为要获取这些整数的字符串,则可以使用String.Split
和Int32.Parse
方法,如;
string s = "25,3";
int startIndex = Int32.Parse(s.Split(',')[0]);
int difference = Int32.Parse(s.Split(',')[1]);
不,你不能。','是一个字符串。你不能把这些组合放在int中,但你可以把它放在字符串中
string inoutSpecifierPosition = (startIndex + "," + difference);
稍后您可以将其再次拆分为int
var integers=inoutSpecifierPosition.Split(',');
int a=int.Parse(integers[0]);
int b = int.Parse(integers[1]);
如果添加逗号,它将不再是整数,而是字符串或双/十进制,具体取决于您的区域性。
让我们假设它是一个字符串。你会想要
var newValue = string.format("{0},{1}", startIndex, difference);
首先,整数没有分数。这是一个整数,所以不能在逗号后面设置任何内容。
其次,您需要类似decimal
:的东西
decimal inoutSpecifierPosition = startIndex + difference / 100; // divide by 100 for example if `difference` can't exceed 100.
或string
获取中的数据
string inoutSpecifierPosition = string.Format("{0},{1}", startIndex, difference);