Solved

how to capitalize first letter and put space between a string

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now