Gondim
Gondim

Reputation: 3048

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("\\/") ?

Upvotes: 1

Views: 938

Answers (3)

Jigar Joshi
Jigar Joshi

Reputation: 240948

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

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

Must See

Upvotes: 4

aeter
aeter

Reputation: 12720

Or with some regex as well:

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

Upvotes: 0

camickr
camickr

Reputation: 324157

Read the String API:

String.substring(...);

Upvotes: 1

Related Questions