Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. a problem about string

a problem about string

Scheduled Pinned Locked Moved C#
questionhelptutorial
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Erdinc27

    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 ?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    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

    E 1 Reply Last reply
    0
    • L Lost User

      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

      E Offline
      E Offline
      Erdinc27
      wrote on last edited by
      #3

      thanks man first one gives the result as i wished..but i didnt get the point what was my mistake

      L 1 Reply Last reply
      0
      • E Erdinc27

        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 ?

        B Offline
        B Offline
        Ben Fair
        wrote on last edited by
        #4

        You should use the method supplied by the .NET Framework called TextInfo.ToTitleCase(); it's in the System.Globalization namespace, the easiest way to acquire the TextInfo object is to retrieve it from the CurrentCulture 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?

        E 1 Reply Last reply
        0
        • E Erdinc27

          thanks man first one gives the result as i wished..but i didnt get the point what was my mistake

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          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 to IndexOf - but then it will still suck and I really recommend my second version (or the build-in function)

          E 1 Reply Last reply
          0
          • L Lost User

            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 to IndexOf - but then it will still suck and I really recommend my second version (or the build-in function)

            E Offline
            E Offline
            Erdinc27
            wrote on last edited by
            #6

            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

            L 2 Replies Last reply
            0
            • B Ben Fair

              You should use the method supplied by the .NET Framework called TextInfo.ToTitleCase(); it's in the System.Globalization namespace, the easiest way to acquire the TextInfo object is to retrieve it from the CurrentCulture 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?

              E Offline
              E Offline
              Erdinc27
              wrote on last edited by
              #7

              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 ?

              B 1 Reply Last reply
              0
              • E Erdinc27

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                if ((i == 0) || (i == text.IndexOf(" "**, i**)+ 1)) Well I said ,i, not [i]

                1 Reply Last reply
                0
                • E Erdinc27

                  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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  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 been true (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)

                  E 1 Reply Last reply
                  0
                  • L Lost User

                    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 been true (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)

                    E Offline
                    E Offline
                    Erdinc27
                    wrote on last edited by
                    #10

                    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 :(

                    L 1 Reply Last reply
                    0
                    • E Erdinc27

                      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 :(

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      You're welcome :thumbsup:

                      1 Reply Last reply
                      0
                      • E Erdinc27

                        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 ?

                        B Offline
                        B Offline
                        Ben Fair
                        wrote on last edited by
                        #12

                        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?

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups