Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

convert first character of string to uppercase

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

String a="how are you";

I need result as How Are Youie, First character and character after space should be in uppercase
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what did you try to realise that?
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sulthan mathina wrote:hi,

String a="how are you";

I need result as How Are Youie, First character and character after space should be in uppercase




Hello Sulthan,
This question is simply related to logic formation since there is no method in JAVA's standard API that would give you this result...

Since logic formation is a homework kind of thing which we are not supposed to do here for anyone... I'm not posting any particular code fragment as a solution.

However, for your help, I believe the following methods of String class :
split(), subString(), toUpper() and the simple concatenation operator should be of use to you. Application of these methods... I leave that part to you only... All the best...

And... oh yes, in case you develop some code and are still stuck with it... simple post it here... someone would sure help you...
 
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is always worthwhile reading the Java™ Tutorials because you often find useful hints there.
 
sulthan mathina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jain,
Thanks 4 your motivation.I found solution without using split, substring ,etc

StringBuffer stringbf = new StringBuffer();
Matcher m = Pattern.compile("([a-z])([a-z]*)",
Pattern.CASE_INSENSITIVE).matcher("how are you");
while (m.find()) {
m.appendReplacement(stringbf,
m.group(1).toUpperCase() + m.group(2).toLowerCase());
}
System.out.println(m.appendTail(stringbf).toString());


I know split,substring but i want different style of code ,thats y i posted this quest. You just check this code Think different !!!
 
Campbell Ritchie
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use StringBuffer, use StringBuilder.

That seems a very complicated way to do it. There are much easier ways to do it in StringBulider without the regular expression.
 
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My only concern with writing complicated-looking code is:

Sure, I understand it when I write it - but two years from now when the VP of the company is standing at my desk wondering why things are not working and I am in a mad dash to fix it.....will it make sense to me then?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use StringBuffer, use StringBuilder.



I don't think there is a choice here -- don't the appendReplacement() and appendTail() methods take a StringBuffer?

Henry
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Philip Persson wrote:
Sure, I understand it when I write it - but two years from now when the VP of the company is standing at my desk wondering why things are not working and I am in a mad dash to fix it.....will it make sense to me then?



This is always true with any regex. And in this case, the only complexity is the regex pattern itself. The find-in-a-while, with append replacement, and append tail should not confuse at all -- as that is how you are supposed to use those three methods together. It's a standard pattern. The only line that different is the second parameter to the append replacement method.

Henry
 
Campbell Ritchie
Marshal
Posts: 79645
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: . . . I don't think there is a choice here -- don't the appendReplacement() and appendTail() methods take a StringBuffer?

Henry

There is a choice; you don't need those methods in the first place. There are far simpler ways to do that exercise.
 
Philip Persson
Greenhorn
Posts: 25
Netbeans IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is a choice; you don't need those methods in the first place. There are far simpler ways to do that exercise.



You mean using regex there are far simpler ways, or using other methods?
 
reply
    Bookmark Topic Watch Topic
  • New Topic