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
Solved

how to capitalize first letter and put space between a string

Posted on 2006-07-11
4
1,180 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
Weekend adv creator 3 52
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
jdbc error in jsp application 20 63
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 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