2010-05-24 52 views

回答

3
String userName = "LICTowner"; 
String prefix = userName.Substring(0,4); // LICT 
String restOfWord = userName.Substring(4); // owner 
5

如果前綴總是4個字母則可以使用Substring方法

在asp.net:

var prefix = username.Substring(0, 4); 

其中第一int是起始索引和第二int是長度。

Substring on MSDN

3

hmmmm ....任何事情之前,A)你應該尋找類似的問題和b)那不是一個很難的問題做......我的意思是,你甚至嘗試? ??

如果前綴始終是4個字母,然後只需使用.Substring方法......作爲

string username; 
string prefix=username.Substring(0,4)// or something like that, cant remember off the top of my head 
+0

我想我會讓任何人對我的言論感到苦惱,但他們是真的,我的意思是,只要按「」。在用戶名將intellisense顯示您的方法列表後,我確信「substring」會響起一個鐘...... – 2010-05-24 05:46:13

+0

+1同意,這是一個很簡單的事情,它已經覆蓋之前在SO以及所有的網絡。讓我們讓這個網站上的問題比c#101的第一天更有挑戰性吧。 – 2010-05-24 08:08:02

1
string s = "LICTowner"; 
    Label1.Text= Regex.Replace(s, "[^A-Z]", ""); 

簡單的正則表達式來刪除比大寫之外的所有字符

0

如果前綴總是4個字符使用簡單的子字符串

其他 a)刪除所有非大寫字符

string s = "LICTowner"; 
     Label1.Text= Regex.Replace(s, "[^A-Z]", ""); 

b)在第一個非大寫字符

Label1.Text= Regex.Split(s, "[^A-Z]")[0]; 
0

您可以使用子串()函數來獲得前四個字符 分裂。

string mystring = "LISTowner"; 
string prefixword = mystring.Substring(0,4); 
Label label1 = new Label(); 
label1.Text = prefixword; 
1
string name="aryan"; 
string prefixwords= name.substring(a,b); 
//a from where you want the string 
// b till where you need the string now take any label and print the value 

string prefixwords= name.substring(0,2); 
label lblmsg= new label(); 
lblmsg=prefixwords.tostring(); // ary 

string restofwords= name.substring(2); // an 
+0

不錯的代碼...感謝你,先生... – 2012-08-29 11:24:00

相關問題