Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

How do i tokenize a string

  Asked By: Lourdes    Date: Sep 22    Category: Java    Views: 907
  

I'm going to split a string into a string with "\n" where each line is no
more than a certain length. Good places to split are " ". The string already has
"\n":s occasionally. So the lines to split are the ones which exceeds a certain
length, in my case 24. This only works once, but each line may have to be
checked many times if it's a really long line. Can this be done with recursion
maybe? This is my code so far. It might be a bit crappy. String newStringBig =
textArea1.getText();
StringTokenizer newTokn = new StringTokenizer(newStringBig,"\n");
int i=newTokn.countTokens();
while(newTokn.hasMoreTokens()) {
String newString= newTokn.nextToken("\n");
if (newString.length()>24)
{
String newString2 = newString.substring(0,newString.lastIndexOf("
",24));
newString=newString2+"\n"+newString.substring(newString.lastIndexOf("
",24))+newTokn.nextToken();
System.out.println(newString);
i=newTokn.countTokens();
}
++i;
}

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Randall Franklin     Answered On: Sep 22

I don't know what you want to do. I didn't even read your
past posting.

Could you tell me what you want to achieve? and maybe also your
sourcecodes?

Fyi, you can do regular expression since jdk 1.4 if i am not wrong.

so why not try that?

 
Answer #2    Answered By: Josie Roberts     Answered On: Sep 22

This is what I want to do:

When a single line  in a text (let's say it's line
wrapped in a TextArea) goes too far let's say a
thousand words without the "\n" it's very hard to
perform band printing in java Graphics2D, as we only
will print the first part of this giant line, as much
as there is space for in the "band". My idea is to
split the line into minor lines, each of a max length
of characters, the same as the "band" allows(I use
monotype so it's OK).

For example: I like my new chevrolet and I drive with
it all day, might become: I like my new chevrolet\n
and I drive with it all day. This has to be made many
times if the string  is very long. Recursively? In that
case, how?

This works  to a degree, but only once, my max length
is here 24. I hope the rest can be understood.

String newStringBig =
textArea1.getText();
StringTokenizer newTokn = new
StringTokenizer(newStringBig,"\n");
int i=newTokn.countTokens();
while(newTokn.hasMoreTokens()) {
String newString= newTokn.nextToken("\n");
if (newString.length()>24)
{
String newString2 =
newString.substring(0,newString.lastIndexOf(" ",24));

newString=newString2+"\n"+newString.substring(newString.lastIndexOf("
",24))+newTokn.nextToken();
System.out.println(newString);
i=newTokn.countTokens();
}
++i;
}

 
Answer #3    Answered By: Marc Anderson     Answered On: Sep 22

If you just want to wrap your text without using StringTokenizer, I think
could give you a little help, here's a code  and comments :
// Retrieve your string
String newStringBig = textArea1.getText();
// Prepare buffer
StringBuffer buffer = new StringBuffer();
// Prepare the limits
int begin = 0;
int end = 0;
// Start the process until we got the end of the string
while (end != (newStringBig.length()))
{
// If the beginning of string  + maximum string (24) bigger than the
length of the string
if ((begin + 24) > (newStringBig.length() - 1))
{
// Okay, it's the end of string, use it as the end limit
end = newStringBig.length();
}
// If the rest of the string still bigger than 24 characters
else


// The end limit is the last occurrence of " " in our partial string
end = newStringBig.lastIndexOf(" ", (begin + 24));
}
// Append the partial string into buffer and add "+"
buffer.append(newStringBig.substring(begin, end) + "\n");
// Ok, we got the string...so just pass " " by adding one space
begin = end + 1;
}
// Show the result
System.out.println(buffer.toString());

Note :
If you have a word with the length  bigger than or equal 24, it's possible
the code would be broken.

 
Answer #4    Answered By: Kiet Jainukul     Answered On: Sep 22

I believe someone has already mentioned that JTextArea(or some similar class)
can do text wrap automatically.

String newStringBig = textArea1.getText();
StringTokenizer newTokn = new StringTokenizer(newStringBig,"\n");
while(newTokn.hasMoreTokens()) {
String newString= newTokn.nextToken("\n");
while (newString.length() > 24){
String line  = newString.substring(0,newString.lastIndexOf(" ",24));
// do thing with the line
// ... ...
// rebuild the string
newString = newString.substring(line.length());
}
// use up the last part less than 24 char length
// ... ...
}

 
Didn't find what you were looking for? Find more on How do i tokenize a string Or get search suggestion and latest updates.




Tagged: