C# .NET - read first all character from string??

Asked By mani on 29-Dec-11 09:30 AM
hi...anybody help me to read first each characer sequence from strintg variable??
dipa ahuja replied to mani on 29-Dec-11 10:10 AM
string name = "dipa ahuja";
string[] names = name.Split(' ');
string result = "";
foreach (string s in names)
{
  result += s[0].ToString() + " ";
}
 
Response.Write("Result: " + result + "<hr>"); //Result d a
Web Star replied to mani on 29-Dec-11 10:15 AM
What do you mean by first all character from string? if you want first char from string than simply get substring as

string str = "This is the complete sentance";
char c = str.Substring(0,1);
// it return first char "T" from string to assign c
Output is
T

//But if you want to get all first character from each word in string than try this
string[] wordsFirstChar = str.Split(' ');

foreach (string word in wordsFirstChar )
{
    Console.WriteLine(word.Substring(0,1));
}

here you will get output first all character in sequance as folllows
Output is
T
i
t
c
s

Riley K replied to mani on 29-Dec-11 08:31 PM


  You can access the characters of a string like array

  
string s = "Its New Year";
 
    Response.Write(s[0]); Output I