capitalize words

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron

    #1

    capitalize words

    string a = "i have an apple";
    i want the output to be "I Have An Apple";


    i was able to capitalize the first word but can't figure out how to
    capitalize every word in that string

    Thanks,
    Aaron


  • W.G. Ryan

    #2
    Re: capitalize words

    a = a.ToUpper();
    http://msdn.microsoft.com/library/de...uppertopic.asp

    HTH,

    Bill

    "Aaron" <kuya789@yahoo. com> wrote in message
    news:O6pGYIdnEH A.324@TK2MSFTNG P11.phx.gbl...[color=blue]
    > string a = "i have an apple";
    > i want the output to be "I Have An Apple";
    >
    >
    > i was able to capitalize the first word but can't figure out how to
    > capitalize every word in that string
    >
    > Thanks,
    > Aaron
    >
    >[/color]


    Comment

    • Bob Grommes

      #3
      Re: capitalize words

      You could do a foreach through the string -- foreach (char c in a) -- or do
      a for loop and use the string's indexer. Either way you can examine each
      character that way and apply Char.ToUpper() to any character following
      whitespace (Char.IsWhiteSp ace()). You can't modify individual characters of
      the string of course without creating a new string, which isn't efficient;
      you might want to append each character to a StringBuilder and return the
      StringBuilder's ToString() at the end of the routine. Or, it might be
      faster to convert the string to a character array, tweak the necessary
      characters, and then convert it back to a string at the end. Your mileage
      will likely vary depending on typical string lengths.

      Yet another approach might be to use the Split() method of string to turn it
      into a string array of words, proper case each word, then put them back
      together into a string. That is conceptually simple but may be too slow if
      you're going to call this routine alot.

      Finally, a regular expression could be constructed to do some or all of this
      work.

      --Bob

      "Aaron" <kuya789@yahoo. com> wrote in message
      news:O6pGYIdnEH A.324@TK2MSFTNG P11.phx.gbl...[color=blue]
      > string a = "i have an apple";
      > i want the output to be "I Have An Apple";
      >
      >
      > i was able to capitalize the first word but can't figure out how to
      > capitalize every word in that string
      >
      > Thanks,
      > Aaron
      >
      >[/color]


      Comment

      • Tom Dacon

        #4
        Re: capitalize words

        If you're using VB.Net, check out the StrConv() function and its Conversion
        argument. VbStrConv.Prope rCase, according to the docs, capitalizes each word
        of a string.

        I'm not aware of a C# equivalent.

        Tom Dacon
        Dacon Software Consulting

        "Aaron" <kuya789@yahoo. com> wrote in message
        news:O6pGYIdnEH A.324@TK2MSFTNG P11.phx.gbl...[color=blue]
        > string a = "i have an apple";
        > i want the output to be "I Have An Apple";
        >
        >
        > i was able to capitalize the first word but can't figure out how to
        > capitalize every word in that string
        >
        > Thanks,
        > Aaron
        >
        >[/color]


        Comment

        • Chris R. Timmons

          #5
          Re: capitalize words

          "Aaron" <kuya789@yahoo. com> wrote in
          news:O6pGYIdnEH A.324@TK2MSFTNG P11.phx.gbl:
          [color=blue]
          > string a = "i have an apple";
          > i want the output to be "I Have An Apple";
          >
          >
          > i was able to capitalize the first word but can't figure out how
          > to capitalize every word in that string[/color]

          Aaron,

          As Tom Dacon pointed out, VB.Net has the StrConv function. What
          isn't generally known is that all of those functions that appear to
          be only callable from VB.Net can be called from any .Net language,
          including C#:


          // Add a reference to the Microsoft.Visua lBasic.dll to your project.

          // For .Net 1.0, it is located in
          // C:\WINDOWS\Micr osoft.NET\Frame work\v1.0.3705

          // For .Net 1.1, it is located in
          // C:\WINDOWS\Micr osoft.NET\Frame work\v1.1.4322


          using System.Globaliz ation;
          using Microsoft.Visua lBasic;

          ....

          string a = "i have an apple";
          Console.WriteLi ne(Strings.StrC onv(a, VbStrConv.Prope rCase,
          CultureInfo.Cur rentCulture.LCI D));

          --
          Hope this helps.

          Chris.
          -------------
          C.R. Timmons Consulting, Inc.

          Comment

          • C# Learner

            #6
            Re: capitalize words

            On Sat, 18 Sep 2004 15:51:56 -0700, Aaron wrote:
            [color=blue]
            > string a = "i have an apple";
            > i want the output to be "I Have An Apple";
            >
            >
            > i was able to capitalize the first word but can't figure out how to
            > capitalize every word in that string[/color]

            Since strings are immutable (for a good reason), you can't say something
            like:

            a[idx] = Char.ToUpper(a[idx]);

            Since this would modify the instance, the assignment can't be made.

            You could, on the other hand, assign the variable a to a whole new string
            instance (reference):

            a = "new string";

            However, you aren't actually changing the instance that a referenced before
            -- you're simple "pointing" a to a new string instance.

            The usual way to work around the immutability problem would be to create a
            StringBuilder instance, modify it as desired then return a string from it:

            StringBuilder sb = new StringBuilder(a );
            sb[idx] = Char.ToUpper(sb[idx]);
            a = sb.ToString();

            Since StringBuilder instances aren't immutable, you *can* modify them, as
            shown.

            In this case, though, I would probably just create a StringBuilder instance
            and fill it up one character at a time:

            /// <summary>
            /// Return a copy of the specified string with the first character and
            /// any characters following spaces, as upper-case.
            /// </summary>
            static string MakeIntoTitleCa se(string val) {
            StringBuilder sb = new StringBuilder() ;

            for (int i = 0; i < val.Length; ++i){
            if (i == 0 || val[i - 1] == ' ') {
            sb.Append(Char. ToUpper(val[i]));
            } else {
            sb.Append(val[i]);
            }
            }

            return sb.ToString();
            }

            Comment

            • Tom Shelton

              #7
              Re: capitalize words

              On Sat, 18 Sep 2004 15:51:56 -0700, Aaron wrote:
              [color=blue]
              > string a = "i have an apple";
              > i want the output to be "I Have An Apple";
              >
              >
              > i was able to capitalize the first word but can't figure out how to
              > capitalize every word in that string
              >
              > Thanks,
              > Aaron[/color]

              using System;
              using System.Globaliz ation;

              class Test
              {
              public static void Main ()
              {
              string s =
              CultureInfo.Cur rentCulture.Tex tInfo.ToTitleCa se ("i have an apple.");
              Console.WriteLi ne (s);
              }
              }
              --
              Tom Shelton [MVP]

              Comment

              • Xarky

                #8
                Re: capitalize words

                Is it possible to traverse the whole string, and when encountering a
                space, set the next character to uppercase. With this method ,it must
                be made sure that only once space exist between two words.

                Maybe there is an easier way how to do it, then someone else will
                post, and both of us will learn :)

                "Aaron" <kuya789@yahoo. com> wrote in message news:<O6pGYIdnE HA.324@TK2MSFTN GP11.phx.gbl>.. .[color=blue]
                > string a = "i have an apple";
                > i want the output to be "I Have An Apple";
                >
                >
                > i was able to capitalize the first word but can't figure out how to
                > capitalize every word in that string
                >
                > Thanks,
                > Aaron[/color]

                Comment

                • Mark Rae

                  #9
                  Re: capitalize words

                  "W.G. Ryan" <WilliamGRyan@g mail.com> wrote in message
                  news:e7PRYadnEH A.2096@TK2MSFTN GP15.phx.gbl...
                  [color=blue]
                  >a = a.ToUpper();
                  > http://msdn.microsoft.com/library/de...uppertopic.asp[/color]

                  That will convert THE ENTIRE STRING TO UPPER CASE, Not Capitalise The First
                  Letter Of Each Word, which is what the OP requires...

                  Fortunately this is very easy in C#:

                  string strTitleCase =
                  System.Globaliz ation.CultureIn fo.CurrentCultu re.TextInfo.ToT itleCase ("the
                  string to be converted");


                  Comment

                  • Cor Ligthert

                    #10
                    Re: capitalize words

                    Aaron,

                    When you use VBNet or import in the other languages the
                    Microsoft.Visua lBasic namespace, you can use both these

                    Console.WriteLi ne(Globalizatio n.CultureInfo.C urrentCulture.T extInfo.ToTitle C
                    ase("i have an apple."))
                    Console.WriteLi ne(StrConv("i have an apple.", VbStrConv.Prope rCase))

                    (with C# closed by ;)

                    I hope this helps?

                    Cor


                    Comment

                    • W.G. Ryan eMVP

                      #11
                      Re: capitalize words

                      Yep, I should have read it more carefully. Thanks, Bill
                      "Mark Rae" <mark@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
                      news:e$D8urhnEH A.3868@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > "W.G. Ryan" <WilliamGRyan@g mail.com> wrote in message
                      > news:e7PRYadnEH A.2096@TK2MSFTN GP15.phx.gbl...
                      >[color=green]
                      > >a = a.ToUpper();
                      > >[/color][/color]
                      http://msdn.microsoft.com/library/de...uppertopic.asp[color=blue]
                      >
                      > That will convert THE ENTIRE STRING TO UPPER CASE, Not Capitalise The[/color]
                      First[color=blue]
                      > Letter Of Each Word, which is what the OP requires...
                      >
                      > Fortunately this is very easy in C#:
                      >
                      > string strTitleCase =
                      > System.Globaliz ation.CultureIn fo.CurrentCultu re.TextInfo.ToT itleCase ("the
                      > string to be converted");
                      >
                      >[/color]


                      Comment

                      Working...