Java String Split with multiple delimiter using pipe '|' -


i trying break string b = "x+yi" 2 integers x , y.

this original answer. here removed trailing 'i' character substring method:

int integerpart = (int)(new integer(b.split("\\+")[0])); int imaginary = (int)(new integer((b.split("\\+")[1]).                       substring(0, b.split("\\+")[1].length() - 1))); 

but found code below works same:

int x = (int)(new integer(a.split("\\+|i")[0])); int y = (int)(new integer(a.split("\\+|i")[1])); 

is there special '|'? looked documentation , many other questions couldn't find answer.

you can use given link understanding of how delimiters works.

how use delimiter in java scanner?

another alternative way

you can use usedelimiter(string pattern) method of scanner class. use of usedelimiter(string pattern) method of scanner class. have used string semicolon(;) tokenize string declared on constructor of scanner object.

there 3 possible token on string “anne mills/female/18″ name,gender , age. scanner class used split string , output tokens in console.

import java.util.scanner;  /*  * java example source code shows how use usedelimiter(string pattern)  * method of scanner class. use string ; delimiter  * use in tokenizing string input declared in scanner constructor  */  public class scannerusedelimiterdemo {      public static void main(string[] args) {          // initialize scanner object         scanner scan = new scanner("anna mills/female/18");         // initialize string delimiter         scan.usedelimiter("/");         // printing delimiter used         system.out.println("the delimiter use "+scan.delimiter());         // printing tokenized strings         while(scan.hasnext()){             system.out.println(scan.next());         }         // closing scanner stream         scan.close();      } } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -