7 Replies Latest reply: Sep 4, 2010 2:09 PM by jschellSomeoneStoleMyAlias RSS

    Is there any Standard API Class or Method to Sentence Case a String ?

    801417
      Hi,
      can anybody tell me pls,

      1) Is there any standard API class or method in Java to Sentence case a string?
      for example i want to make Resultant String from the in hand Original String,
      Original String : this is a sample string
      Resultant String : This is a sample string ( note : the first letter of the sentence is capitialized)

      2) Is there any standard API class or method in Java to capitalize every word in a sentence ?
      for example i want to make Resultant String from the in hand Original String,
      Original String : this is a sample string
      Resultant String : This Is A Sample String (note : the first letter of every word is capitialized)

      --Sujoy                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        • 1. Re: Is there any Standard API Class or Method to Sentence Case a String ?
          Kayaman
          Nope, but it's not too hard to do it by hand.
          The first case is trivial and the second one can be done with String.split() etc.
          • 2. Re: Is there any Standard API Class or Method to Sentence Case a String ?
            843789
            The following approach is the one I would take but if you are doing this as homework then I doubt if it is acceptable.
            import e.util.Rewriter;
            
            /**
             * For Rewriter see http://elliotth.blogspot.com/2004/07/java-implementation-of-rubys-gsub.html
             */
            public class CamelCaseTest
            {
                static class CamelRewriter extends Rewriter
                {
                    CamelRewriter()
                    {
                        super("\\b([a-zA-Z])");
                    }
            
                    public String replacement()
                    {
                        return group(1).toUpperCase();
                    }
                }
            
                static class SentenceRewriter extends Rewriter
                {
                    SentenceRewriter()
                    {
                        super("(?<=(?:^|\\.) {0,10})([a-zA-Z])");
                    }
            
                    public String replacement()
                    {
                        return group(1).toUpperCase();
                    }
                }
            
                public static void main(String[] args) throws Exception
                {
                    String line = "the quick . brown fox jumps. over the lazy dog";
                    System.out.println(line);
                    {
                        CamelRewriter rewriter = new CamelRewriter();
                        String updatedline = rewriter.rewrite(line);
                        System.out.println(updatedline);
                    }
                    {
                        SentenceRewriter rewriter = new SentenceRewriter();
                        String updatedline = rewriter.rewrite(line);
                        System.out.println(updatedline);
                    }
                }
            }
            P.S. For some reason every time I propose the use of Rewriter I am ignored.
            • 3. Re: Is there any Standard API Class or Method to Sentence Case a String ?
              Kayaman
              sabre150 wrote:
              P.S. For some reason every time I propose the use of Rewriter I am ignored.
              Interesting (not that you're being ignored, but the Rewriter class).

              Still, it has very little hope of becoming a Standard Java Idiom? because it's missing from the standard API. Or maybe squeeze it into apache commons..
              • 4. Re: Is there any Standard API Class or Method to Sentence Case a String ?
                843789
                Kayaman wrote:
                sabre150 wrote:
                P.S. For some reason every time I propose the use of Rewriter I am ignored.
                Interesting (not that you're being ignored, but the Rewriter class).

                Still, it has very little hope of becoming a Standard Java Idiom? because it's missing from the standard API. Or maybe squeeze it into apache commons..
                A good use for Rewriter comes up in these forums about every couple of months. This is my second call on it in the last week but in the other case Darryl's suggestion of using String.format() is more applicable.

                Can't remember who it is but somebody in these forums advocates doing the same as Rewriter is doing behind the scenes but using copy and paste. To me this seems silly but ...
                • 5. Re: Is there any Standard API Class or Method to Sentence Case a String ?
                  jschellSomeoneStoleMyAlias
                  sabre150 wrote:
                  P.S. For some reason every time I propose the use of Rewriter I am ignored.
                  Global conspiracy to suppress hearsay?

                  Might note that I have used perl for years, actually started before java existed, and I probably only every used the replacement sub functionality less than 5 times. So I would think it just isn't that useful, besides being rather difficult to conceptualize versus more hokey but easier to follow implementations.
                  • 6. Re: Is there any Standard API Class or Method to Sentence Case a String ?
                    801417
                    Thank you all for the replies. As I am from a PHP background, there are 2 simple inbuild functions in PHP to do the above two tasks, that's why I was quering about availability of any standard method in Java just like PHP.

                    I have done the first one using Siting.charAt(0).toUpperCase and concatenating the rest substring as it is with the first capitalized character.
                    And done the 2nd one using StringTokenizer to break with " ", and them appling toUpperCase() of String, and then appended Strings to create a resultant sentence String.

                    ---Sujoy
                    • 7. Re: Is there any Standard API Class or Method to Sentence Case a String ?
                      843789
                      me_sujoy wrote:
                      Thank you all for the replies. As I am from a PHP background, there are 2 simple inbuild functions in PHP to do the above two tasks, that's why I was quering about availability of any standard method in Java just like PHP.

                      I have done the first one using Siting.charAt(0).toUpperCase and concatenating the rest substring as it is with the first capitalized character.
                      And done the 2nd one using StringTokenizer to break with " ", and them appling toUpperCase() of String, and then appended Strings to create a resultant sentence String.

                      ---Sujoy
                      Once again my suggestion to use Rewriter has been ignored other than as part of a general 'thank you'. OK, if it is homework then Rewriter would probably not be applicable but I would like to think that people would acknowledge the suggestion and say why it is not applicable. Maybe I expect too much!