2017-04-24 12 views
0

我有一個HTML Textarea,我想將它轉換爲帶分號作爲分隔符的單行字符串。用分號替換HTML Textarea中的新行

例如我有僱員數目在HTML文本區域的列表:

------------- 
|100000001 | 
|100000002 | 
|100000003 | 
|100000004 | 
|100000005 | 
|100000006 | 
|100000007 | 
|100000008 | 
|100000009 | 
|100000010 | 
------------- 

我希望它被轉換成字符串的一行,並用分號替換新行,並把它傳遞給一個變量。

它應該是這樣的:

;100000001;100000002;100000003;100000004;100000005;100000006;100000007;100000008;100000009;100000010 

我怎樣才能做到這一點?

或者您還有其他建議使用什麼來代替HTML Textarea,用戶可以在其中輸入員工編號列表並允許從剪貼板粘貼值。

任何意見將不勝感激。提前致謝。

+0

用';'代替行尾('\ r \ n','\ n') – TriV

+0

您想在SQL或c#中執行它? – Krishna

+0

@Krishna C#... – theo

回答

1

你可以在C#中做到這一點:

var result = yourText.Replace("\r\n", ";").Replace("\r", ";").Replace("\n", ";"); 
0

如果你需要在SQL本身生成這個,那麼你可以這樣做,在SUBSTRINGLEN函數的幫助下。

Create table sample(
data varchar(max) 
) 

insert into sample 
values(10000010), 
(10000020), 
(10000030), 
(10000040), 
(10000050), 
(10000060), 
(10000070), 
(10000080) 

declare @temp varchar(max) 
declare @temp2 varchar(max) 
set @temp = '' 
select @temp = @temp + data + '; ' from sample 

select SUBSTRING(@temp,0,LEN(@temp)) 
1

,如果你想在SQL Server然後

select convert(varchar,EmployeeNumber)+';' from EmployeesTable for xml path('') 

做,如果你想在C#中這樣做,那麼

txtMyTextBox.Text = string.Join(";", txtMyTextBox.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));