• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert first character of string to uppercase  RSS feed

 
sulthan mathina
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what did you try to realise that?
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56529
172
  • Mark post as helpful
  • send pies
  • 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.
 
Philip Persson
Greenhorn
Posts: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56529
172
  • Mark post as helpful
  • send pies
  • 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: 20
Firefox Browser Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!