java 从java中的句号拆分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12901365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:42:38  来源:igfitidea点击:

split from full stop in java

java

提问by heshan

Possible Duplicate:
The split() method in Java does not work on a dot (.)

可能的重复:
Java 中的 split() 方法对点 (.)

I'm new to java. I want to split a String from "." (dot) and get those names one by one. But this program gives error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"please help me

我是 Java 新手。我想从“.”中拆分一个字符串。(点)并一一获取这些名称。但是这个程序出错了: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"请帮帮我

 String input1 = "van.bus.car";

 System.out.println(input.split(".")[0]+"");

 System.out.println(input.split(".")[1]+"");

 System.out.println(input.split(".")[2]+"");

回答by Rohit Jain

In regex, Dot(.)is a special meta-character which matches everything.

在正则表达式中,Dot(.)是一个特殊的元字符,它匹配everything.

Since String.splitworks on Regex, so you need to escape it with backslash if you want to match a dot.

由于String.split适用于正则表达式,因此如果要匹配dot.

System.out.println(input.split("\.")[0]+"");

To learn more about Regex, refer to following sites: -

要了解有关 Regex 的更多信息,请参阅以下站点:-

回答by pb2q

The argument to splitis a regex, and so the full stop/dot/.has a special meaning: match any character. To use it literally in your split, you'll need to escape it:

的参数split是一个正则表达式,所以句号/点/.有一个特殊的含义:匹配任何字符。要在拆分中按字面使用它,您需要将其转义:

String[] splits = input1.split("\.");

That should give you an array of length 3 for your input string.

这应该为您的输入字符串提供一个长度为 3 的数组。

For more about regex and which characters are special, see the docs for Pattern.

有关正则表达式的更多信息以及哪些字符是特殊的,请参阅Pattern文档