• Post Reply Bookmark Topic Watch Topic
  • New Topic

String split with white space  RSS feed

 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my requirement,
want to split the string if its exceed the length limit and to wrap without breaking word in next line

example:
String name="New York Language Center-Bronx,Inc";

want to wrap in two lines if the length is >15

Expected:
New York Language
Center-Bronx,Inc

please suggest me
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suresh dhanapal wrote:This is my requirement,
want to split the string if its exceed the length limit and to wrap without breaking word in next line
please suggest me

After 38 posts, I hope you know by now that we won't write the code for you, suresh.

So: What have you tried? How do you think it should be done?

Try writing out the problem as if you were going to explain it to a child with no knowledge of Java. What would you tell them to do? And if you like 'visualising', try thinking of children's letter blocks that you have to fit into a frame that can only take 15 of them in a line.

The fact is that there are many ways you could do this; but just for starters, I'd suggest having a look at the String.split() method, and see if you can work out how that might help you.

HIH

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
The fact is that there are many ways you could do this; but just for starters, I'd suggest having a look at the String.split() method, and see if you can work out how that might help you.


I don't know if I agree with this. Regular expressions is not easy, and finding one that would split on a whitespace but based on the size of the results to be created, is not easy.

Henry
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston,
String.split() doesn't work for me, I am just exploring the possibility without using any loop.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suresh dhanapal wrote:Winston,
String.split() doesn't work for me, I am just exploring the possibility without using any loop.

ItDoesntWorkIsUseless

I'm not sure Winston was suggesting using String.split to do the whole thing as Henry seems to think.
I was thinking of splitting the String into tokens and then building the lines up from these tokens, inserting spaces or new lines as appropriate, but you would obviously need a loop for that. Why do you not want to use a loop ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:I don't know if I agree with this. Regular expressions is not easy, and finding one that would split on a whitespace but based on the size of the results to be created, is not easy.

Well, apart from the correct regex for splitting "words" (which is likely to depend on suresh's definition of a "word"), what that will give you is an array of words as Strings, which you could then concatenate based on length.

However:
@suresh: If you really want to do the whole thing yourself, you may have to go through each character in your input String individually. Just remember that some idiot might put two (or more) spaces between "words". You'll have to decide whether you want to handle that or not yourself.

suresh dhanapal wrote:String.split() doesn't work for me, I am just exploring the possibility without using any loop.

I'm pretty sure that won't fly. Part of your solution is going to be to know whether the current word will "fit" on the current line, so somehow you're going to have to extract "words", in order, from your input String. And for that, you'll need a loop of some sort.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:I was thinking of splitting the String into tokens...

That occurred to me too (great minds obviously think alike ), so:

@suresh - Yet another alternative: You could do that with a Scanner (java.util.Scanner ←click) based on your input String; but that involves learning the API for a whole class, not just one method. It will, however, give you "words" the way I think you want them, as tokens.

You'll still need to use a loop to get them though.

Winston
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along with the others I invite you to have a go at this yourself first.

However, I have found a couple of contradictions between your requirement and example.
suresh dhanapal wrote:want to wrap in two lines if the length is >15

Expected:
New York Language
Center-Bronx,Inc

You say you want to wrap if the line length exceeds 15 characters, but "New York Language" is 17 characters. "Center-Bronx,Inc" has no whitespace to wrap on but the 'word' is 16 characters and you don't define what you want to do in this scenario.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
suresh dhanapal wrote:want to wrap in two lines if the length is >15

Expected:
New York Language
Center-Bronx,Inc

You say you want to wrap if the line length exceeds 15 characters, but "New York Language" is 17 characters. "Center-Bronx,Inc" has no whitespace to wrap on but the 'word' is 16 characters and you don't define what you want to do in this scenario.


I actually attempted something like this years ago -- finding a regular expression for a single call to split(), that would split based on both delimiters, and based on size. I actually came close, but ran into the same issues that Tim mentioned. It also had edge conditions (rounding errors), and caused it to sometimes not work well.

With so many edge conditions, it is probably best to just uses a loop, and calls to substring() to extract the found components... once you define what to do in all the edge cases, of course...

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:With so many edge conditions, it is probably best to just uses a loop, and calls to substring() to extract the found components... once you define what to do in all the edge cases, of course...

And then the real fun is justifying it. As in 'line-width' justify; not justifying it to your peers - that's something completely different.
I wrote an awk program to do it for 80-character screens about...oooh...twenty years ago now.

Winston
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

maybe this topic can be of any help:

http://www.coderanch.com/t/635447/java/java/Dividing-string-substrings

where someone stepped in with some dédain ;)

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:where someone stepped in with some dédain ;)

Dédain? Moi?

Winston
 
suresh dhanapal
Ranch Hand
Posts: 40
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all your response.
Finally I have only option to do with split and loop



got the expected string format(always I will get not more than 30 char length)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Piet Souris wrote:where someone stepped in with some dédain ;)

Dédain? Moi?

Winston

Honestly, I didn't mean you! But this makes it even better

But I'm glad OP solved his problem. Well done!

Greetz,
Piet
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
But I'm glad OP solved his problem. Well done!


Agreed. And thanks for coming back and reporting the solution too. Have a cow.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And FYI... if you want a solution with only a split() call, you can probably do something like this...



It does work for your example. Unfortunately, it also has all the edge condition issues that I mentioned earlier, so use with a grain of salt.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suresh dhanapal wrote:Finally I have only option to do with split and loop...

Very well done!

Just one little nitpick for future reference (but I suspect you weren't to know):

String[] schNameTokens = schName.split("\\s+"); // Note the '+'

Will split "words" delimited by any number of spaces, which is generally preferable. If someone happens to put two spaces between words in your input String, your split() call will create an empty "word".

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using \\s+ and joining words with a single space will effectively change everything to uniform spacing.
Can you use a StringJoiner to put your Strings back together again?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!