java 分割字符串问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 17:31:04
String fileRealname="D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5";
String [] arr=fileRealname.split("\\");
System.out.println(arr[0]);
我这样运行过了,结果出错了
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\

byf1987_gmail, 谢谢你了,能说明一下原理吗?
^

String [] arr=fileRealname.split("\\\\");

import java.util.*;

public class Test {

public static void main(String s[]){

String fileRealname=" D:|Program Files|Apache Software Foundation|Tomcat 5.5";
StringTokenizer st=new StringTokenizer(fileRealname,"|");

String temp = st.nextToken();
System.out.println(temp);

temp =temp + st.nextToken();
System.out.println(temp);

temp = temp + st.nextToken();
System.out.println(temp);

temp =temp + st.nextToken();
System.out.println(temp);

}
}

我不知道你是不是这个意思

String [] arr=fileRealname.split("\\\\");

原因吗,是因为java转义符\\ 代表\

split

返回的是数组fileRealname.split("\");

查 String的 API吧.

String[] str = fileRealname.split("\\"),“\”要转义。

String