How can we cut a string ? In Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How can we cut a string ? In Java

String a = "SPcoder" how to get just coder from variable 'a'?

19th Sep 2018, 12:12 PM
Googel
Googel - avatar
5 Answers
+ 6
You can use substring: public String substring(int startIndex); public String substring(int startIndex, int endIndex);
19th Sep 2018, 12:18 PM
aelbler
aelbler  - avatar
+ 5
Thanks aelbler
19th Sep 2018, 12:45 PM
Googel
Googel - avatar
+ 3
Assumuning you dont want to hard code it, use—> a = substring(a.indexOf(“coder”)); The substring returns the string, starting from the index position of your parameter, and the indexOf method returns the index of the start of the first occurunce of the String segment inside the string. returning a -1 if the String segment does not exist
19th Sep 2018, 1:20 PM
Vivan Shah
Vivan Shah - avatar
+ 3
use StringBuffer class
20th Sep 2018, 4:53 PM
Aarav Raj
Aarav Raj - avatar
+ 2
Vivan Shah But that won't work all the time. substring gets you to the first character of the substring, but the way you do it, there is no end to the substring.
19th Sep 2018, 10:12 PM
J.G.
J.G. - avatar