Results 1 to 6 of 6

Thread: Separating a string in java

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Separating a string in java

    Hello,
    I need to cut a string into several pieces but retaining the separator in pieces.
    example:
    my chain "or (varname) is a (Subject)
    Expected Result: [ "be", "(varname)", "being one", "(Subject)]
    separators are the brackets ().
    I tried several solutions (split, StringTokenizer ...) but I can only have [ "be", "varname", "being one", "Subject"]
    Someone would have an idea? Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Separating a string in java

    Hello,
    Just be your own separation method using indexOf indexOf ('{') and ('}'). You search the '(', if you think you are looking for the ')' nearest you make a substring and do it again for the rest of the chain. I think this should take long time to code such a program , it is quit simple. If you need help about the class and the methods provided by the java you can find then on the official site of the sun.

  3. #3
    Join Date
    Nov 2009
    Posts
    356

    Re: Separating a string in java

    Hello,
    You can use regular expressions, but if I do not control enough to find the solution. I will still file the snippet I just tested:

    Code:
    String s = "This is (variable) (I) variable2 sing";
    List l = findVars(chain);
    System.out.System.out.println(l);
    Here is the code
    Code:
    @ SuppressWarnings("unchecked")
    Public static List findVars(String input) {
    		Pattern p = Pattern.compiles("\\{.*\\}");
    Matcher m = p.matcher(input);
    List list = new ArrayList();
    		if (m.find()) {
    			do {
    				list.add(m.group());
    				
    			} while (m.find());
    			return list;
    		}
    		return null;
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Separating a string in java

    Hello,
    This is not the solution to the problem in question, but just for info, the "*" RegExp is the default "greedy" (greedy), i.e that "{.*} in a string like "(variable) is my () variable2" will matcher whole chain, including the first ')' encountered. One can remedy this by adding "?" after the *, which gives {.*?} which allow to stop the very first ")" found.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Separating a string in java

    Hello,
    Here is the code that can help you, just go through it
    Code:
    	private List <String> spwtdel(String str, String OPENLiMiT, String closeLimit)
    	{
    		List lt = <String> new ArrayList <String>();
    		int froplm = str.indexOf(OPENLiMiT);
    		int frcllm = str.indexOf(closeLimit, froplm);
    		while ((froplm! = -1) && (frcllm! = -1))
    		{
    			if (froplm> 0)
    				lt.add(str.substring(0, FirstOpenLimit));
    lt.add(str.substring(froplm, frcllm + 1));
    			
    			if (frcllm <str.length())
    				str = str.substring(frcllm + 1, Str.length());
    			else
    				str = "";
    froplm = str.indexOf(OPENLiMiT);
    frcllm = str.indexOf(closeLimit, froplm);			
    		}
    		if (str.length() > 0)
    			lt.add(str);
    		return lt;
    	}

  6. #6
    Join Date
    Nov 2009
    Posts
    335

    Re: Separating a string in java

    Hello,
    Here is another program that will interest you. Have a look at it
    Code:
    List <String> split(String string) {
            ArrayList str = <String> new ArrayList <String>();
    
    Pattern pat = Pattern.compiles("\\{(.*?)\\}");
    Matcher mat = pat.mat(string);
    				
    	int ls = 0;
    	while (mat.find()) {
    	  str.add(string.substring(ls, match.home()));
              str.add(mat.group());
    ls = mat.end();
    	}
    	if (ls! = string.length())
    	  str.add(string.substring(ls, string.length()));
     
             return str;
    }

Similar Threads

  1. Definition of a String in Java?
    By cloud101 in forum Software Development
    Replies: 5
    Last Post: 28-07-2014, 10:14 AM
  2. The String Class in Java
    By blueprats in forum Guides & Tutorials
    Replies: 3
    Last Post: 12-03-2010, 02:44 PM
  3. String to uppercase in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 12:33 AM
  4. Java String test
    By Aidan 12 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 02:22 PM
  5. How to convert string into int in Java
    By Zool in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,573,225.83101 seconds with 17 queries