java String.split(“.”)和String.split(“|”)的注意事项

2019-03-10 10:50|来源: 网路

查jdk的api你可以发现split的参数是正则表达式,如果你直接使用.或|来切分,是不对的

解决方案是使用\ 行转义


String.split(".") 改为String.split("\\.")

String.split("|") 改为String.split("\\|")



相关问答

更多
  • 他们基本上是马课程。 Scanner适用于需要解析字符串的情况,并提取不同类型的数据。 它是非常灵活的,但可以说是不能给你最简单的API,只需得到一个由特定表达式分隔的字符串数组。 String.split()和Pattern.split()给你一个简单的语法来做后者,但这基本上是他们所做的一切。 如果要解析生成的字符串,或者根据特定的令牌更改分隔符,则不会帮助您。 StringTokenizer比String.split()更加限制,也有一点使用。 它基本上设计用于拉出由固定子串限定的令牌。 由于这个限制 ...
  • $是正则表达式中的特殊字符 (表示“行尾”)。 为了使它简单,你需要逃避它,例如 "\\$" , "[$]" 或使用引号"\\Q$\\E" 。 $ is special character in regex (it means "end of a line"). To make it simple literal you need to escape it, for example with "\\$", "[$]" or using quotations "\\Q$\\E".
  • 如何使用正则表达式\"(d+),(d+)\" 。 然后使用Pattern.matcher(input)而不是String.split ,并通过Matcher.group(int)获取您的数字。 请考虑以下代码段: String line = "\"1,31\""; Pattern pattern = Pattern.compile("\"(\\d+),(\\d+)\""); Matcher matcher = pattern.matcher(line); if (matcher.matches()) { ...
  • 您应该首先尝试将文件缩小到足够小以使其正常工作。 这将允许您评估您有多大的问题。 其次,你的问题肯定与String#split无关,因为你一次只在一行上使用它。 Vertex和Edge实例的Vertex是什么。 您将不得不重新设计一个更小的占用空间,或彻底检查您的算法,以便只能在内存中使用图形的一部分,其余部分在磁盘上。 PS只是一般的Java注释:不要写 String s1 = new String(tokens[0]); String s2 = new String(tokens[1]); 您只需 S ...
  • 你正则表达式匹配整个字符串。 因此,在拆分时,整个字符串被删除。 它与"a".split("a")完全相同,它返回一个空数组。 您可以使用的是: queryText.replaceAll(".*name `([^`]+)`.*", "$1") 返回some name 。 You regex matches the whole string. Thus, when splitting, the whole string gets removed. It is exactly the sam ...
  • 你应该逃避它: String words[] = word.split("\\|"); 在类似的问题在这里检查这个解释: 为什么String.split需要管道分隔符被转义? 字符串对象的split()方法有一个正则表达式作为参数。 这意味着一个非转义的| 不是解释为字符而是解释为OR,意思是“空字符串或空字符串”。 You should escape it: String words[] = word.split("\\|"); Check this explanation in similar qu ...
  • | 需要这样逃脱 \\| 应该做的伎俩 | needs to be escaped so \\| should do the trick
  • 如果你想split字符'(' , ')' , ','和' ' ,你需要传递一个匹配其中任何一个的正则表达式。 最简单的方法是使用一个字符类: String[] array = a.split("[(), ]"); 通常,正则表达式中的括号是分组运算符,如果您希望将它们用作文字,则必须对其进行转义。 但是,在字符类分隔符内,不必转义括号字符。 If you want split to split on the characters '(', ')', ',', and ' ', you need to pa ...
  • 试试看:hexdump -C test.txt如果你有linux,你可以看到你不可打印的字符。 修剪()也很好。 Just try with: hexdump -C test.txt if you have linux, you can see the non-printable chars you have. Also the trim() answer it's fine.
  • 我修改了代码并对其进行了测试。 它的工作原理(不要忘记避免硬编码“,”因此您可以将该函数用于任何分隔符): private static String[] split(String delim, String line) { StringTokenizer tokens = new StringTokenizer(line, delim, true); String previous = delim; Vector v = new Vector(); while (tok ...