how to create sub strings from a string in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to create sub strings from a string in java?

Hello everyone! Hope you having a great day so far. I'm fairly new to java and I was given an assignment to create sub strings from a larger string. They gave an example: String s = "welcometojava" has the following lexicographically-ordered substrings of length k = 3 ["ava","com","elc","eto","jav","lco","met","oja","ome","toj","wel"] After they got the sub strings, they ask to return the smallest and largest which would be "ava" and "wel". Can you guys please give me an idea on how to approach this, I was thinking of using a for loop to iterate through the string but i had no luck with that. All answers are appreciated!

17th Feb 2019, 6:07 PM
Leo Hunter
Leo Hunter - avatar
1 Answer
+ 1
String sub = YourString.substring (beginIndex, endIndex); beginIndex is included, endIndex not In your example: String sub = s.substring (0, k); //wel For the next word you need a new begin- and endIndex Int begin = 0 --> begin+=k Int end = k --> end+=k I would use a for loop --> create substring --> put it in a list or an array (to sort them) --> change the values for begin and end (maybe you need a condition that end <= s.length () https://www.javatpoint.com/substring
17th Feb 2019, 7:06 PM
Denise Roßberg
Denise Roßberg - avatar