2

I want to show company name in code word like Dell Computers = DC , but getting error in concatenating string.

         string str = txtCompanyname.Text.Trim();

            string[] output = str.Split(' ');
            foreach (string s in output)
            {
               // Console.Write(s[0] + " ");
                Response.Write(s[0]);
                string newid += s[0].ToString();//getting error here

            }

4 Answers 4

4

Define NewID outside the foreach loop. You can't define and assign the variable at the same time.

 string str = txtCompanyname.Text.Trim();
 string[] output = str.Split(' ');
 string newid= string.empty;
 foreach (string s in output)
  {
     // Console.Write(s[0] + " ");
        Response.Write(s[0]);
        newid += s[0].ToString();//getting error here

  }
Sign up to request clarification or add additional context in comments.

Comments

0
        string str = txtCompanyname.Text.Trim();

        string[] output = str.Split(' ');
        foreach (string s in output)
        {

            Response.Write(s);
            string newid += s;

        }

Comments

0

You can do it using linq.

Just split the full name and select first character of every part;

string str = "Dell Computers";
var abbr = new string(str.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
    .Select(x => char.ToUpper(x[0]))
    .ToArray());

Comments

0

You can use LINQ to do this:

public string FirstLetters(string s)
{
    return String.Join("", s.Trim().Split(' ').Select(w => w.Substring(0, 1).ToUpper()));
}

var result = FirstLetter(txtCompanyname.Text);

This code does the following:

  1. Trims your string
  2. Splits your string by whitespace into a collection of separate words
  3. Generates a collection by taking first uppercase letter from every word
  4. Combines the collection of letters to a string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.