简体   繁体   English

不使用.split命令分割字符串

[英]Splitting a String without .split command

I got a String "1/3" where I want to get only the number 1 我有一个字符串“ 1/3”,我只想得到数字1

Is it possible to get it without using "1/3".split("\\\\/") ? 是否可以不使用“ 1/3” .split(“ \\\\ /”)获得它?

you can use indexOf() and subString() to get 1 您可以使用indexOf()subString()获得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");

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

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