tags:

views:

74

answers:

6

i have a username= LICTowner.

i need to get the prefix from the word LICTowner i.e LICT.

how to split the word and get the 4 letter prefix.

in asp.net using C#

+5  A: 

if the prefix is ALWAYS 4 letters you can use the Substring method:

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

where the first int is the start index and the second int is the length.

Substring on MSDN

Darko Z
+3  A: 
String userName = "LICTowner";
String prefix = userName.Substring(0,4); // LICT
String restOfWord = userName.Substring(4); // owner
Strelok
+3  A: 

hmmmm.... before anything, a) you should really look for similar questions and b) thats not a hard problem to do... i mean, have you even tried???

if the prefix is always 4 letters, then just use the .Substring method... as in

string username;
string prefix=username.Substring(0,4)// or something like that, cant remember off the top of my head
Francisco Noriega
We'll I guess I made whomever down-voted bitter with my remarks, but they are true, i mean, just by pressing "." after username will have intellisense show you the list of methods, im sure "substring" would ring a bell...
Francisco Noriega
+1 Agreed, This is such a simple thing and it has been covered before on SO as well as all over the web. Lets keep the questions on this site a little more challenging than the first day of c# 101.
Daniel Dyson
+1  A: 
  string s = "LICTowner";
  Label1.Text=    Regex.Replace(s, "[^A-Z]", "");

Simple regular expression to remove all characters other than upper case

josephj1989
A: 

If the prefix is always 4 characters use simple substring

Else a) remove all non upper case characters

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

b) split at the first non upper case character

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

You can use the Substring() function to get the first four Character.

string mystring = "LISTowner"; string prefixword = mystring.Substring(0,4); Label label1 = new Label(); label1.Text = prefixword;

subodh