Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String Capitilization

Posted on 1998-04-08
4
Medium Priority
?
190 Views
Last Modified: 2012-05-04
How can I capitilize the first character of the first word in a sentence?  I have random numbers working with string arrays making sentences.  I can't figure out how to capitilize the first letter of the first word?  Here's my code to print the strings:

g.drawString( article[a] + " " + noun[b] + " " + verb[c] + " " + preposition[d] + " " + article[e]
                       + " " + noun[f] + ".", 25, yPos );
0
Comment
Question by:thorner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 40 total points
ID: 1233997
Try:

char c[1];
c[1] = (char)article[a].substring(1,1) - char("a") + char("A");
String s(c);
s += article[a].substring(2);

g.drawString( s + " " + noun[b] + " " + verb[c] + " " + preposition[d] + " " + article[e]
                           + " " + noun[f] + ".", 25, yPos );
0
 
LVL 16

Expert Comment

by:imladris
ID: 1233998
Wouldn't it be better to use the toUpperCase function in String?

g.drawString(article[a].substring(0,1).toUpperCase()+article[a].substring(1)+" "+noun[b]..... etc.


0
 

Author Comment

by:thorner
ID: 1233999
Thank you.  Both of these methods worked, however I liked the second one better because it was a shorter alternative to the first.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1234000
imladris,

Yes it would :-)  Looks like I still have some C++ vices in my coding that keep me looking  for increses execution time tricks.

In Java your solution is in fact better.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question