how i can do this in c#
Explosive;a dynamic person
if i do split then ; is removed from text
how i can make them
Explosive;A dynamic pers开发者_Python百科on
string s = "Explosive;a dynamic person";
var separatorIndex = s.IndexOf(";");
if (separatorIndex != -1 && separatorIndex < s.Length + 1)
s = s.Substring(0, separatorIndex + 1) + s.Substring(separatorIndex + 1, 1).ToUpper() + s.Substring(separatorIndex + 2);
Console.WriteLine(s);
gives "Explosive;A dynamic person"
I guess you would like something more general, but it is hard to figure out from the question how general you want it.
This should work:
string str = "Explosive;a dynamic person";
int poz = str.IndexOf(";");
string part1 = str.Substring(0, poz + 1);
string part2 = str.Substring(poz + 1);
str = part1 + part2[0].ToString().ToUpper() + part2.Substring(1);
You would need to rejoin the results of the split using the same delimiter, ;
in this case.
string foo = "Explosive;a dynamic person";
string bar = string.Join(";",
foo.Split(';')
.Select(s =>
s.Length == 0 ? s : s.Substring(0, 1).ToUpper() + s.Substring(1))
.ToArray());
Console.WriteLine(bar); // "Explosive;A dynamic person";
(And if you're using .NET 4 then you can omit the final ToArray
call.)
Looks like you want to:
- Split the string
- Capitalize the first character of each token
- Join the results into a new string
This ought to do it:
string original = "Explosive;a dynamic person";
string[] split = original.Split(';');
string[] capitalized = split.Select(s => CapitalizeFirstLetter(s)).ToArray();
string joined = string.Join(";", capitalized);
string CapitalizeFirstLetter(string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
char[] chars = str.ToCharArray();
char[0] = char.ToUpper(char[0]);
return new string(chars);
}
This may not be the most elegant solution, but it works for your string:
var str = "String;a string!";
var index = str.IndexOf(";") + 1;
var newString = str.Substring(0,index);
if( str[index] != ' ' )
{
newString += Char.ToUpper(str[index]);
newString += str.Substring( index + 1 );
}
Console.WriteLine(newString);
精彩评论