cancel
Showing results for 
Search instead for 
Did you mean: 

Splitting a string into 4 equal parts

Former Member
0 Kudos

Hi All,

I have a string with maximum no of characters as 100,

I want to split this string into 4 equal parts.

Any help will be appreciated.

Regards

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Hope the following Threads will help you regarding your problem.

Similar thread from SDN may help you out.

https://forums.sdn.sap.com/click.jspa?searchID=17937952&messageID=6342839

Thanks.

Nitesh

Answers (3)

Answers (3)

Former Member
0 Kudos

hi!

chk this thread[]

former_member192434
Active Contributor
0 Kudos

Hi Rajeev,

Use this sample code

class SplitString {

public static void main(String[] arguments) {

StringTokenizer ex1, ex2; // Declare StringTokenizer Objects

int count = 0;

String strOne = "one two three four five";

ex1 = new StringTokenizer(strOne); //Split on Space (default)

while (ex1.hasMoreTokens()) {

count++;

wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is" + ex1.nextToken() );

}

count = 0; // Reset counter

String strTwo = "item one,item two,item three,item four"; // Comma Separated

ex2 = new StringTokenizer(strTwo, ","); //Split on comma

while (ex2.hasMoreTokens()) {

count++;

wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is "+ ex2.nextToken() );

}

}

}

Thanks

Anup

Edited by: Anup Bharti on Oct 27, 2008 12:36 PM

Former Member
0 Kudos

Hi,

Following is a sample code may be of use for you....



String string = "abcdefgh";
		int length = string.length();
		int splitLength = length / 4;
		int beginIndex = 0;
		int endIndex = splitLength;
		for(int x=0; x<4; x++)
		{
			String str = string.substring( beginIndex, endIndex);
			beginIndex = endIndex;
			endIndex = endIndex + splitLength;
			System.out.println(str);
		}

Regards

Ayyappraj