Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Char(13)
Message
De
06/10/2002 18:00:57
 
Information générale
Forum:
Java
Catégorie:
Autre
Titre:
Divers
Thread ID:
00701574
Message ID:
00708229
Vues:
11
>I've got a nasty String() I need to break down into lines and I know it's got char(13)'s or char(10)'s in there somewhere.
>
>How do I detect these in Java?
>
>I can do this in any number of other languages ... Java's got me stumped!

Here is an example:
package linetokenizer;

import java.util.StringTokenizer;
import java.util.Vector;

public class LineTokenizer {
  public final static String CRLF = "\r\n";
  public final static String CR = "\r";
  public final static String LF = "\n";

  //Construct the application
  public LineTokenizer() {};

  //Break the string into lines and return as a vector
  public Vector Tokenize( String input ) {
    Vector v = new Vector();

    StringTokenizer st1 = new StringTokenizer( input, CRLF );
    while ( st1.hasMoreTokens() ) {
      StringTokenizer st2 = new StringTokenizer( st1.nextToken(), CR );
      while ( st2.hasMoreTokens() ) {
        StringTokenizer st3 = new StringTokenizer( st2.nextToken(), LF );
        while ( st3.hasMoreTokens() )
          v.add( st3.nextToken() );
      }
    }
    return v;
  }

  //Main method
  public static void main( String[] args ) {
    StringBuffer sb = new StringBuffer();

    sb.append( "Line 1" );
    sb.append( CRLF );
    sb.append( "Line 2" );
    sb.append( CR );
    sb.append( "Line 3" );
    sb.append( LF );
    sb.append( "Line 4" );
    sb.append( LF );
    sb.append( "Line 5" );

    LineTokenizer lt = new LineTokenizer();
    Vector v = lt.Tokenize( sb.toString() );

    for ( int i = 0; i < v.size(); i++ )
      System.out.println( v.elementAt( i ) );
  }
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform