Solved

how to capitalize first letter and put space between a string

Posted on 2006-07-11
4
1,181 Views
Last Modified: 2010-05-18
Hi

I get the state names from a link in this format

alabama
northCarolina etc

I capitalize the first letter using this code

String capitalize_chapterName = chapter_name.length()>1? chapter_name.substring(0,1).toUpperCase() + chapter_name.substring(1).toLowerCase() : chapter_name.toUpperCase();
it works fine for one word sattes
for northCarolina i need it to be North Carolina

Any help will be greatly appreciated
0
Comment
Question by:huzefaq
[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
4 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 17085765
you can capitalize the first letter using the method that you stated, for other letters, you could use the method isUpper in java.lang.Character.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html


create a string buffer and then loop through the string, character by character, and if the letter is uppercase, then add a space and the letter to the buffer, otherwise just add the letter.

using the character class, you can even perform more fine tunning like removing whitespaces, determining if the character is a letter or a digit, etc.
0
 
LVL 92

Expert Comment

by:objects
ID: 17086809
try:

s = s.replaceAll("([A-Z])", " {1}");
0
 
LVL 24

Accepted Solution

by:
slyong earned 500 total points
ID: 17087881
java regex might be helpful here:

import java.util.regex.*;

public class test
{
  public static void main(String args[]) {
    String state = "northCarolina";
    Pattern p = Pattern.compile("[A-Z]"); // Matching all Upper Charaters...
    Matcher m = p.matcher(state);
    while (m.find()) {
      state = state.substring(0,m.start()) + " " + state.substring(m.start());  // Add one space to all the Upper Characters..
    }
    state = state.substring(0,1).toUpperCase() + state.substring(1); // Convert the First Character to Upper...
    System.out.println(state);
  }
}
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17088215
I havn't compiled...chk if it works for u

str="northCarolina"
StringBuffer  = new (str);
if (Character.isLowerCase(strbuf.charAt( 0)))
{
     Character.toUpperCase(strbuf.charAt( 0))
}

for ( i = 0;  i < strbuf.length();  i++ )
 {        
 
        ch = strbuf.charAt( i );
        if (Character.isUpperCase(ch))
        {
             strbuf.insert(i , " ");
        }
 }
 str=strbuf.toString();
 System.out.println(strbuf);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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