Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

split String with "[" as delimeter

801912Jun 30 2007 — edited Jun 30 2007
Hello,

I want to know how I can split a String variable by the square brackets (open and close) as delimeters. Here's my SSCCE. The problem is that I don't know how to make the square brackets seem as literal variables and not what they're normally used for in regular expressions. When I use a backslash before each square bracket, I get compile errors becuz they are the wrong escape characters.
Also, I eventually need to display the result of this split as a String array. Is there a way to do this?

Please help me out. Thanks!
public class MyStringTest {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "[A, B, C]";
		String [] splittedStr = str.split("[|]");
		for(int i=0;i<splittedStr.length;i++)
		{
			System.out.println(splittedStr);

}
System.out.println(splittedStr);
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 28 2007
Added on Jun 30 2007
4 comments
1,570 views