0

I'm having a string as following in Java. The length of the string is not known and as an example it will be something like below.

String str = "I love programming. I'm currently working with Java and C++." 

For some requirement I want to get first 15 characters. Then 30, 45, 70 next characters. Once the string was split if the name was not meaningful then it should be split from nearest space. For the above example output is as following.

String strSpli1 = "I love "; //Since 'programming' is splitting it was sent to next split String strSpli2 = "programming. I'm currently ";//Since 'working' is splitting it was sent to next split String strSpli3 = "working with Java and C++."; 

Please help me to achieve this.

Updated answer for anybody having this kind of requirement.

 String str = "I love programming. I'm currently working with Java and C++."; String strSpli1 = ""; String strSpli2 = ""; String strSpli3 = ""; try { strSpli1 = str.substring(15); int pos = str.lastIndexOf(" ", 16); if (pos == -1) { pos = 15; } strSpli1 = str.substring(0, pos); str = str.substring(pos); try { strSpli2 = str.substring(45); int pos1 = str.lastIndexOf(" ", 46); if (pos1 == -1) { pos1 = 45; } strSpli2 = str.substring(0, pos1); str = str.substring(pos1); try { strSpli3 = str.substring(70); int pos2 = str.lastIndexOf(" ", 71); if (pos2 == -1) { pos2 = 45; } strSpli3 = str.substring(0, pos2); str = str.substring(pos2); } catch (Exception ex) { strSpli3 = str; } } catch (Exception ex) { strSpli2 = str; } } catch (Exception ex) { strSpli1 = str; } 

Thank you

6
  • What have you tried yourself and is not working? Commented May 12, 2021 at 6:42
  • You can share what you have tried so far ? You can use the indexOf method of String class to find the next space characters that are near to your length of 30,45,70 and split the string using substring. Commented May 12, 2021 at 6:42
  • @Nisanth Reddy I have updated the question with the code that I've tried. Commented May 12, 2021 at 6:53
  • @AbRe you shouldn't just split based on length alone. Try doing it step by step. Split with the first length 30. Then check this substring that you have split if it is breaking a word in the middle. If it is breaking the word, then find the two nearest ` ` in your string using indexOf. Once you have those 2 indices, you can decide which one to choose and update your new substring. Then you can proceed to the next length you want and repeat the same process until you reach the end of the string. Commented May 12, 2021 at 6:57
  • How do you decide "if the name was not meaningful" ? Commented May 12, 2021 at 7:21

2 Answers 2

2

use the 2 parameter version of lastIndexOf() to search for space backwards starting from a given position. Example for the first 15 characters:

int pos = str.lastIndexOf(" ", 16); if (pos == -1) { pos = 15; } String found = str.substring(0, pos); str = str.substring(pos+1); 

this is missing checks like ensuring the string starts with at least 15 characters, or that pos+1 is valid for given length


suggest having a look at java.text.BreakIterator

Sign up to request clarification or add additional context in comments.

Comments

-1

why you use so many try catch ? just try this.

public class MyClass { public static void main(String args[]) { String str = "I love programming. I'm currently working with Java and C++."; String strSpli1 = ""; String strSpli2 = ""; String strSpli3 = ""; strSpli1 = str.substring(0, 7); strSpli2 = str.substring(7, 33); strSpli3 = str.substring(34, str.length()); System.out.println(strSpli1+"\n"); System.out.println(strSpli2+"\n"); System.out.println(strSpli3+"\n"); } 

use substring(start index, end index).

4 Comments

because I don't know the length of the string. If the length is less than 15 it will throw an exception
use this for know how many of length , str.length()
do you mean your input is general or dynamic?
Dynamic input and sometimes it will have less than 15 length

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.