?
Solved

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

Posted on 2006-07-21
7
Medium Priority
?
256 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 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