简体   繁体   中英

Splitting a String without .split command

I got a String "1/3" where I want to get only the number 1

Is it possible to get it without using "1/3".split("\\\\/") ?

you can use indexOf() and subString() to get 1

String str = "1/3";
System.out.println(str.substring(0, str.indexOf("/")));  

Must See

阅读String API:

String.substring(...);

Or with some regex as well:

String s = "1/3";
String m = s.replaceAll("(\\d+)/.+", "$1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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