Splitting a String without .split command
I got a String "1/3" where I want to get only 开发者_StackOverflow社区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
- Javadoc
Read the String API:
String.substring(...);
Or with some regex as well:
String s = "1/3";
String m = s.replaceAll("(\\d+)/.+", "$1");
精彩评论