2014-11-03 63 views
-2

我有兩個整數值,例如253。現在我想將這些添加到一個整數中,但條件是這兩個值應該用逗號(,)分隔。它可能又豈是done.It是給我的錯誤是逗號不能以整數來conactenated .. 我已經試過這樣的..如何從字符串獲得整數的逗號

int inoutSpecifierPosition = (startIndex + "," + difference); 

,但它給我錯誤..Please幫助我。 ..

任何建議將不勝感激...

+0

你應該看看像「數字字符串轉換」和主題「字符串連接」。 – 2014-11-03 07:53:15

+4

逗號在整數?什麼?你確定你明白什麼是整數? – 2014-11-03 07:53:22

回答

5

不可能有像25,3這樣的整數。它可以是string而不是像;

string inoutSpecifierPosition = startIndex + "," + difference; 

沒有整數可以有任何逗號或小數點分隔符或千位分隔符。他們是只是號碼。只有他們的字符串表示可以有。這就是爲什麼你

現在我想這些內容添加到一個單一的整數,但條件是這兩個值應該用逗號分開

說法是沒有意義的。

由於string + int返回string而不是int,您的代碼給出錯誤。

在.NET Framework中,有3 +運算符重載字符串連接。

從C#規格$7.8.4 Addition operator

string operator + (string x, string y); 
string operator + (string x, object y); 
string operator + (object x, string y); 

二進制+運算的這些重載執行字符串連接。 如果字符串連接的操作數爲空,則替換爲空字符串 。 否則,通過調用從類型對象繼承的虛擬ToString方法 將任何非字符串參數轉換爲其 字符串表示形式。

如果你想將你的整數格式化爲一個字符串,你可以使用string.Format like;

string s = string.Format("{0},{1}", startIndex, difference); // 25,3 

如果已經25,3只要你想獲得這些整數的字符串,可以使用String.SplitInt32.Parse方法一樣;

string s = "25,3"; 
int startIndex = Int32.Parse(s.Split(',')[0]); 
int difference = Int32.Parse(s.Split(',')[1]); 
0

如果添加逗號它不會是一個整數了,它會成爲一個字符串,或雙/小數取決於你的文化。

讓我們假設它是一個字符串。你會想要

var newValue = string.format("{0},{1}", startIndex, difference); 
0

首先,整數沒有分數。這是一個整數,所以你不能在逗號後面設置任何內容。

其次,你需要的東西就像一個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); 
1

你就是不行。 ','是一個字符串。你不能在INT把這些combinly但你可以把這個字符串

string inoutSpecifierPosition = (startIndex + "," + difference); 

以後你可以把它分成INT再次

var integers=inoutSpecifierPosition.Split(','); 
int a=int.Parse(integers[0]); 
int b = int.Parse(integers[1]); 
相關問題