2

I just beginning to learn java, so please don't mind. I have string

String test="John Software_Engineer Kartika QA Xing Project_Manager Mark CEO Celina Assistant_Developer"; 

I want to splitting based of position of Company={"Software_Engineer", "QA","Project_Manager","CEO ","Assistant_Developer"};

EDITED:

if above is difficulties then is it possible??? Based or {AND, OR)

String value="NA_USA >= 15 AND NA_USA=< 30 OR NA_USA!=80" String value1="EUROPE_SPAIN >= 5 OR EUROPE_SPAIN < = 30 " 

How to split and put in hashtable in java. finally how to access it from the end. this is not necessary but my main concern is how to split.

Next EDIT:

I got solution from this, it is the best idea or not????

String to="USA AND JAPAN OR SPAIN AND CHINA"; String [] ind= new String[]{"AND", "OR"}; for (int hj = 0; hj < ind.length; hj++){ to=to.replaceAll(ind[hj].toString(), "*"); } System.out.println(" (=to=) "+to); String[] partsparts = to.split("\\*"); for (int hj1 = 0; hj1 < partsparts.length; hj1++){ System.out.println(" (=partsparts=) "+partsparts[hj1].toString()); } 

and

 List<String> test1=split(to, '*', 1); System.out.println("-str333->"+test1); 

New EDIT:

If I have this type of String how can you splitting:

final String PLAYER = "IF John END IF Football(soccer) END IF Abdul-Jabbar tennis player END IF Karim -1996 * 1974 END IF"; 

How can i get like this: String [] data=[John , Football(soccer) ,Abdul-Jabbar tennis player, Karim -1996 * 1974 ]

Do you have any idea???

8
  • 3
    It's not a very good idea to have every name and position on the same line without any sort of delimiter besides space. This will make a split infeasible; you'd be better served parsing the string yourself. Commented May 16, 2014 at 4:58
  • Check my edited question, it that feasible to split. Commented May 16, 2014 at 5:00
  • @user3643373 Not really, because it'll be quite difficult for a program to tell that "QA" is a position and not a name, without a list of positions (unless you're going by all capital letters) Commented May 16, 2014 at 5:01
  • So, what is the alternative solution for this?any idea. Commented May 16, 2014 at 5:04
  • Yes. Put delimiters in your data. Then, you can actually do proper splitting on it. Commented May 16, 2014 at 5:05

5 Answers 5

1

This will split your string for you and store it in a string array(Max size 50).

 private static String[]split = new String[50]; public static void main(String[] args) { String test="John -Software_Engineer Kartika -QA Xing -Project_Manager Mark -CEO Celina -Assistant_Developer"; for (String retval: test.split("-")){ int i = 0; split[i]=retval; System.out.println(split[i]); i++; } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Do you understand my question or not? please see my question again, thanks for your response sir. I want to split based on certain string not space, according to space , it is easier and lots of tutorials found in internet but based on my question . i am not found, so that i create my question.
Hello okay well it would be easier to add a '/' or '-' to the string where you want to split it.
Thank you your response, you idea lots of help for me, but please tell me how do you add - in every, position string. I pulled this string from web database. it can not changed. please see my newedit bold part in question.
1

You can make a string with Name:post and space. then it will be easy get desire value.

String test="John:Software_Engineer Kartika:QA Xing:Project_Manager" 

Comments

1

I am unable to comment as my reputation is less. Hence i am writing over here.

Your first Question of String splitting could be generalized as positional word splitting. If it is guaranteed that you require all even positioned string, you could first split the string based on the space and pull all the even position string.

On your Second Question on AND & OR split, you could replace all " AND " & " OR " with single String " " and you could split the output string by single space string " ".

On your third Question, replace "IF " & " END" with single space string " " and I am not sure whether last IF do occurs in your string. If so you could replace it too with empty string "" and then split the string based on single space string " ".

First classify your input string based on patterns and please devise an algorithm before you work on Java.

I would suggest you to use StringBuffer or StringBuilder instead of using String directly as the cost is high for String Operation when compared to the above to.

Comments

0

try this

String[] a = test.replaceAll("\\w+ (\\w+)", "$1").split(" "); 

here we first replace word pairs with the second word, then split by space

3 Comments

Ok sure , sir. please wait my response.
@Evgeniy Dorofeev Can you please explain your answer in brief? It would make your answer more useful for others.
yes i am agree with @MohitJain , because i am beginners, difficult to understand how to exactly work it.
0

You can take a set which have all positions Like

Set<String> positions = new HashSet<String>(); positions.add("Software_Engineer"); positions.add("QA"); String test="John Software_Engineer Kartika QA Xing Project_Manager Mark CEO Celina Assistant_Developer"; List<String> positionsInString = new ArrayList<String>(); Iterator<String> iterator = positions.iterator(); while (iterator.hasNext()) { String position = (String) iterator.next(); if(test.contains(position)){ positionsInString.add(position); break; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.