Solved

how to capitalize first letter and put space between a string

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 37
How to convert String matching to regex in java 4 33
type mismatch (Object[] to double[] 4 24
hibernate example issues from command prompt 10 42
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

810 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