Results 1 to 3 of 3
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default String Split method

    After a lot of java programs with Swings/awt i decide to make some programs running on console just for practise...
    and i face up a strange problem about split method on Strings..i have a text file which i read from my PC and i want to split it and to store all the words inside an Array
    but for some reasons the split method of the text (which is not null or empty) returns an empty array..

    here is a small example of what i find out ..

    this code is working fine! :

    Java Code:
    public class SplitTest{
    	private String document;
    	
    	public SplitTest(){
    		setDocument();
    		splitMethod();
    	}
    	
    	public void setDocument(){
    		document = "sad,asD,asd,we,wAe,asd,we,tTt,";
    	}
    	
    	private void splitMethod(){
    		document = document.toLowerCase(); 
    		String [] documentTokens = document.split(",");
    		System.out.println(documentTokens.length);
    	}
    	
    	public static void main(String args []){
    		new SplitTest();
    	}
    }
    results on console is : 8


    but this one is not working at all.. returns an empty array and the output on console is 0

    Java Code:
    public class SplitTest{
    	private String document;
    	
    	public SplitTest(){
    		setDocument();
    		splitMethod();
    	}
    	
    	public void setDocument(){
    		document = "sad.asD.asd.we.wAe.asd.we.tTt.";
    	}
    	
    	private void splitMethod(){
    		document = document.toLowerCase(); 
    		String [] documentTokens = document.split(".");
    		System.out.println(documentTokens.length);
    	}
    	
    	public static void main(String args []){
    		new SplitTest();
    	}
    }
    i found this problem when i read the text and i use this methods (on my program the above are just examples) to remove all non words characters :

    Java Code:
         document = document.toLowerCase();
    		document = document.replaceAll(",",".");
    		document = document.replaceAll("-",".");
    		document = document.replaceAll(" ",".");
    		document = document.replaceAll("\t",".");
    		document = document.replaceAll("-",".");
    		document = document.replaceAll("'",".");
    		document = document.replaceAll("!",".");
    		document = document.replaceAll(";",".");
    		String [] documentTokens = document.split(".");
    		System.out.println(documentTokens.length);
    this code takes the text for example "I'am trying to solve this problem, it's really strange!"
    and make it like this --> "i.am.trying.to.solve.this.problem.it.s.really.str ange."
    and the to split it but the length is 0 ... like the example above..

    thx in advance
    Last edited by ShadowWalker; 05-29-2014 at 03:45 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: String Split method

    The dot has a special meaning in a regex. Try preceeding the dot with a double backslash.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: String Split method

    Oh omg... yeah you right.. pfff thx you very much !!

Similar Threads

  1. Replies: 7
    Last Post: 04-28-2012, 03:10 AM
  2. Using the split() method in the string class
    By Jdelacroix in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 11:39 AM
  3. String split method
    By DavidG24 in forum New To Java
    Replies: 5
    Last Post: 12-14-2010, 03:01 PM
  4. String split method
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 12:06 AM
  5. Please help me with Split method> String
    By javanew in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •