cancel
Showing results for 
Search instead for 
Did you mean: 

Reg: String splitting

Former Member
0 Kudos

Hi,

I have a string like title-name.

i want to capture only title into another string.

how can i do it. kindly suggest.

Accepted Solutions (1)

Accepted Solutions (1)

former_member192434
Active Contributor
0 Kudos

Hi Prasanthi

You can either of the following code, for spliting and capturing the string.

1.

StringTokenizer st = new StringTokenizer("this is a test");

while (st.hasMoreTokens()) {

wdComponentAPI.getMessageManager().reportSuccess(st.nextToken());

}

2.

class StringSplit {

public static void main(String args[]) throws Exception{

new StringSplit().doit();

}

public void doit() {

String s3 = "Real-How-To";

String [] temp = null;

temp = s3.split("-");

dump(temp);

}

public void dump(String []s) {

wdComponentAPI.getMessageManager().reportSuccess("----


");

for (int i = 0 ; i < s.length ; i++) {

wdComponentAPI.getMessageManager().reportSuccess(s<i>);

}

wdComponentAPI.getMessageManager().reportSuccess("----


");

}

}

Thanks

Anup

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi just impliment this code

String text = "title-name"

String[] words=text.split("-"); // this splits at delimiter.

String title = words[0];

//then in title string you can capture your titlename

Regards

Raghu

Former Member
0 Kudos

Hi prasanthi,

Use the folllowing code

String s1 = "abc-def-ghi-jkl";

String []s2 = s1.split(String regex);

Your regex contains the "-" string.

You can get any value from s2 by giving the index number.

Hope this will help you.

Regards

Sagar Ingalwar

Former Member
0 Kudos

what should i do to capture the string till the first occurance of

"-".

former_member192434
Active Contributor
0 Kudos

Hi Prasanthi,

Try to use StringTokenizer and then capture it into String.

As I posted sample code above.

Thanks

Anup

Former Member
0 Kudos

Hi reddy,

If you have string "title-name" stored in AC ,you can split the string in following way,

int in = AC.indexOf("-");

String AC1 = AC.substring(0,in);

Hope this will help;

Regards,

Prajakta