a problem about string
-
hi guys...i have a problem about string usage..i want to make capital letters of a text in the textbox after space...for example if textbox is entered like that mert efe i want to write like that Mert Efe..to do that i wrote that codes public string Cevir(string text) { string temizlenmis = null; for (int i = 0; i < text.Length; i++) { if ((i == 0) || (i==text.IndexOf(" ")+1)) temizlenmis += char.ToUpper(text[i]); else temizlenmis += char.ToLower(text[i]); } return temizlenmis; } and it works for two words but if i enter third word it doesnt work as i wished...it works like that if i enter three words like mert efe demir and it gives result Mert Efe demir so what is wrong here ?
-
hi guys...i have a problem about string usage..i want to make capital letters of a text in the textbox after space...for example if textbox is entered like that mert efe i want to write like that Mert Efe..to do that i wrote that codes public string Cevir(string text) { string temizlenmis = null; for (int i = 0; i < text.Length; i++) { if ((i == 0) || (i==text.IndexOf(" ")+1)) temizlenmis += char.ToUpper(text[i]); else temizlenmis += char.ToLower(text[i]); } return temizlenmis; } and it works for two words but if i enter third word it doesnt work as i wished...it works like that if i enter three words like mert efe demir and it gives result Mert Efe demir so what is wrong here ?
You are searching for the first space every time.. How about (untested)
public string TitleCase(string text)
{
StringBuilder result = new StringBuilder();
bool nextIsCap = true;
for (int i = 0; i < text.Length; i++)
{
if (nextIsCap)
{
result.Append(char.ToUpper(text[i]));
nextIsCap = false;
}
else
result.Append(char.ToLower(text[i]));
if (text[i] == ' ')
nextIsCap = true;
}
return result.ToString();
}edit: ok that sucked, next try: (also untested)
public static string ToTitleCase(string text)
{
char[] buffer = text.ToCharArray();
bool nextIsCap = true;
for (int i = 0; i < buffer.Length; i++)
{
if (nextIsCap)
buffer[i] = char.ToUpper(buffer[i]);
else
buffer[i] = char.ToLower(buffer[i]);
nextIsCap = buffer[i] == ' ';
}
return new string(buffer);
}modified on Thursday, January 7, 2010 8:41 AM
-
You are searching for the first space every time.. How about (untested)
public string TitleCase(string text)
{
StringBuilder result = new StringBuilder();
bool nextIsCap = true;
for (int i = 0; i < text.Length; i++)
{
if (nextIsCap)
{
result.Append(char.ToUpper(text[i]));
nextIsCap = false;
}
else
result.Append(char.ToLower(text[i]));
if (text[i] == ' ')
nextIsCap = true;
}
return result.ToString();
}edit: ok that sucked, next try: (also untested)
public static string ToTitleCase(string text)
{
char[] buffer = text.ToCharArray();
bool nextIsCap = true;
for (int i = 0; i < buffer.Length; i++)
{
if (nextIsCap)
buffer[i] = char.ToUpper(buffer[i]);
else
buffer[i] = char.ToLower(buffer[i]);
nextIsCap = buffer[i] == ' ';
}
return new string(buffer);
}modified on Thursday, January 7, 2010 8:41 AM
-
hi guys...i have a problem about string usage..i want to make capital letters of a text in the textbox after space...for example if textbox is entered like that mert efe i want to write like that Mert Efe..to do that i wrote that codes public string Cevir(string text) { string temizlenmis = null; for (int i = 0; i < text.Length; i++) { if ((i == 0) || (i==text.IndexOf(" ")+1)) temizlenmis += char.ToUpper(text[i]); else temizlenmis += char.ToLower(text[i]); } return temizlenmis; } and it works for two words but if i enter third word it doesnt work as i wished...it works like that if i enter three words like mert efe demir and it gives result Mert Efe demir so what is wrong here ?
You should use the method supplied by the .NET Framework called
TextInfo.ToTitleCase()
; it's in theSystem.Globalization
namespace, the easiest way to acquire theTextInfo
object is to retrieve it from theCurrentCulture
like so:System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("mert efe");
MSDN Reference: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx[^]
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
-
thanks man first one gives the result as i wished..but i didnt get the point what was my mistake
How about the second one, it should give the same result (right?) Your mistake is that you search for the first space, you could fix it be adding
,i
(yes that's all) to the call toIndexOf
- but then it will still suck and I really recommend my second version (or the build-in function) -
How about the second one, it should give the same result (right?) Your mistake is that you search for the first space, you could fix it be adding
,i
(yes that's all) to the call toIndexOf
- but then it will still suck and I really recommend my second version (or the build-in function)no man the second one didnt give the same result..it doesnt make capital first letter of first word...and where should i add the "i" ?? i used like if ((i == 0) || (i == text.IndexOf[i](" ")+ 1)) and if ((i == 0) || (i == text.IndexOf(" ")[i]+ 1)) but it given error
modified on Thursday, January 7, 2010 9:27 AM
-
You should use the method supplied by the .NET Framework called
TextInfo.ToTitleCase()
; it's in theSystem.Globalization
namespace, the easiest way to acquire theTextInfo
object is to retrieve it from theCurrentCulture
like so:System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("mert efe");
MSDN Reference: http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx[^]
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
hii Ben thanks for you reply..i tried the method u suggested it is really good method..but i want to ask one more question about it...for example we have capital i like "İ" in turkish but it writes like I when it makes it capital..i think it is because of TextInfo myTI = new CultureInfo("en-US",false).TextInfo; so how i can use turkish characters instead of english ?
-
no man the second one didnt give the same result..it doesnt make capital first letter of first word...and where should i add the "i" ?? i used like if ((i == 0) || (i == text.IndexOf[i](" ")+ 1)) and if ((i == 0) || (i == text.IndexOf(" ")[i]+ 1)) but it given error
modified on Thursday, January 7, 2010 9:27 AM
-
no man the second one didnt give the same result..it doesnt make capital first letter of first word...and where should i add the "i" ?? i used like if ((i == 0) || (i == text.IndexOf[i](" ")+ 1)) and if ((i == 0) || (i == text.IndexOf(" ")[i]+ 1)) but it given error
modified on Thursday, January 7, 2010 9:27 AM
I tested my second function, it works fine edit: to be fair, when I first posted it it had a bug that made the first letter lower case, it was just that it said
bool nextIsCap = false
which obviously should have beentrue
(but hey that's no reason to just forget about that function, it's a very simple fix and I fixed it the next minute or so) -
I tested my second function, it works fine edit: to be fair, when I first posted it it had a bug that made the first letter lower case, it was just that it said
bool nextIsCap = false
which obviously should have beentrue
(but hey that's no reason to just forget about that function, it's a very simple fix and I fixed it the next minute or so) -
thanks man i am really appreciated because of your help.. take care and thanks again but my method doesnt work again even if write "i" as u said :(
-
hii Ben thanks for you reply..i tried the method u suggested it is really good method..but i want to ask one more question about it...for example we have capital i like "İ" in turkish but it writes like I when it makes it capital..i think it is because of TextInfo myTI = new CultureInfo("en-US",false).TextInfo; so how i can use turkish characters instead of english ?
erdinc27, You'll need to get the code letters for the Turkish culture, I'm not sure what they are but you should be able to find them rather quickly by searching the 'Net. Once you have those code letters you'll replace the "en-US" code with the Turkish code, then it will operate on the string using the Turkish alphabet.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?