[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to check if a String has one word or two words

Posted on 2006-07-21
7
Medium Priority
?
267 Views
Last Modified: 2010-03-31
Hi

I have a string which have state name
like
Arizona
californinia
South carolina
North carolina

what I need is to check

if(state is one word{

get standard abbreviation

}
else
{

get first letter of both the words

}

Would anyone know how to do this

Any help will be greatly appreciated
0
Comment
Question by:huzefaq
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 17154583
You could keep a Map of states, then

                  Pattern p = Pattern.compile("^(.).+\\b(.).+");
                  Matcher m = p.matcher(inputString);
                  if (m.matches()) {
                        String letterOne = m.group(1);
                        String letterTwo = m.group(2);
                        System.out.printf("%s %s\n", letterOne, letterTwo);
                  }
                  else {
                        String state = stateMap.get(inputString);
                  }
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17155064
you can try this too,

if(state.indexOf(' ') > 0) {
// The state has more than 1 words.
}
else {
}

This works efficiently if the state has words seperated by one/more spaces
0
 

Author Comment

by:huzefaq
ID: 17155108
CEHJ Thanks

But

 System.out.printf("%s %s\n", letterOne, letterTwo);

this line is giving error

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:huzefaq
ID: 17155125
Ajay-Singh

How can I get the first leter of both the words then
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17155210
>>System.out.printf("%s %s\n", letterOne, letterTwo);

You can delete that
0
 
LVL 2

Expert Comment

by:RoyalNepal
ID: 17155511
You can do like this...too.



if (state.equals (""))//if state string is empty dont do anything
      return;

state = state.trim(); //takes out the spaces from first or last index

if(state.indexOf(' ') != -1)
{
  //return abbrev..it has only one word
}
else
{
//it has two words....so to take out the first letter..
//eg...if it has "South carolina" it returns South
  int index = state.indexOf(' ');
  return state.substring(0,index+1);
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17156150
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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