java的分裂功能 [英] java split function

查看:156
本文介绍了java的分裂功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我了解如何拆分在java中works.I有以下code

 字符串temp_array [];
    字符串利率=RF \\\\ \\\\ 0.6530 \\\\ 0.6535 D;
    串临时= rates.substring(1,rates.length());
    的System.out.println(TEMP); //输出˚F\\ 0.6530 \\ 0.6535 \\ D
    字符串的正则表达式=\\\\;
    temp_array = temp.split(正则表达式);
    串insertString =INSERT成表值(+ temp_array [0] +,+ temp_array [1] +,+ temp_array [2] +,+ temp_array [3] +);

但在分割功能我得到下面的异常

 异常线程mainjava.util.regex.PatternSyntaxException:意外的内部接近指数1错误
\\
 ^
    在java.util.regex.Pattern.error(来源不明)
    在java.util.regex.Pattern.compile(来源不明)
    在java.util.regex.Pattern中的<&初始化GT;(未知来源)
    在java.util.regex.Pattern.compile(来源不明)
    在java.lang.String.split(来源不明)
    在java.lang.String.split(来源不明)
    在simple_hello.main(simple_hello.java:15)


解决方案

当你键入\\\\,这其实是一个反斜杠(因逃逸的特殊字符Java中的字符串)。

正前pressions还用反斜杠作为特殊字符,你需要用另一个反斜线进行转义。所以,最后,你需要通过\\\\\\\\的模式来匹配一个反斜杠。

Can somebody help me in understanding how split in java works.I have the following code

String temp_array[];           
    String rates = "RF\\0.6530\\0.6535\\D";
    String temp = rates.substring(1, rates.length());
    System.out.println(temp);// prints F\0.6530\0.6535\D
    String regex = "\\";
    temp_array = temp.split(regex);
    String insertString = "INSERT into table values("+temp_array[0]+","+temp_array[1]+","+temp_array[2]+","+temp_array[3]+")";

however at the split function i get the following exception

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at simple_hello.main(simple_hello.java:15)

解决方案

When you type "\\", this is actually a single backslash (due to escaping special characters in Java Strings).

Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\\\" as pattern to match a single backslash.

这篇关于java的分裂功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆