简体   繁体   English

同时使用和。 作为扫描器类中的定界符

[英]Using both , and . as delimiter in scanner class

I have read all the already posted questions but was unable to understand the syntax I know we have to use | 我已经阅读了所有已经发布的问题,但是无法理解我知道我们必须使用的语法。 but I don't know how to. 但我不知道该怎么办 I have no idea of regex. 我不知道正则表达式。 So pls tell me the complete statement of the useDelimiter function of scanner class. 因此,请告诉我扫描仪类的useDelimiter函数的完整说明。

I was trying to separate a,bc,de, into abcde Using next() of scanner class 我正在尝试使用扫描仪类的next()将a,bc,de分为abcde

The argument passed to useDelimiter is a regular expression. 传递给useDelimiter的参数是一个正则表达式。 So in this case, you can just use an expression like "[,.]" which matches either , or . 因此,在这种情况下,你可以使用像一个表达"[,.]"这两种匹配,. .

You can read entire String using Scanner . 您可以使用Scanner读取整个String Then you will have some like following 然后您将有一些类似的内容

 String str="a,b.c,d.e,";

Now you can use replaceAll() method to get what you want. 现在,您可以使用replaceAll()方法获取所需的内容。

 String str="a,b.c,d.e,";
 System.out.println(str.replaceAll(",|\\."," "));

Out put: 输出:

a b c d e 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM