splitting string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashamyth1
    New Member
    • Nov 2011
    • 1

    splitting string

    hai,

    hai i have constant maximum length... Based on that maximum length i have to split one string ..how to do in java...

    Example:


    String number= "00010105110521 0100010545";

    spilt into 00010/1051105/210100/010545 like this .. this should be constant assigned to one variable




    Regards
    Asha
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    #2
    I would do it as follows: convert the string into a character array and then using a for-loop, insert a "/" at every 4th position (if your counter starts at 0)
    Code:
    	public static String divideString(String sampleString, int breakPosition){
    		
    		char[] charString = sampleString.toCharArray();
    		String newString = "";
    		
    		for(int i = 0; i < charString.length; i++){
    			if(charString.length % breakPosition == 0)
    				newString+="/";
    			newString += charString[i];
    		}
    		return newString;
    	}
    that should do it for you...

    Comment

    Working...