The Java Method to Split Lines

Java programs often read text from external files, importing the content for processing within an application. Sometimes a Java program will also use imported text to present information to users within an application interface. Text from an external file may contain new line characters, indicating the line breaks within the content. To split such text into separate lines, programmers can use the string class split method.

  1. String Split Method

    • The Java string class provides methods for processing and accessing information about the characters in a section of text. The split method takes a string value and a regular expression to match, dividing the string into two each time the regular expression is matched. If the split method performs on a text string with new line characters in it, with these characters indicated as part of the regular expression, it will return a string array with each text line stored within an array element. The array length will be the same as the number of lines in the text string.

    Regular Expression

    • The string split method can take a variety of expressions. The following demonstrates a simple example where a single space character is acting as the regular expression to split the string on:

      String someTextInfo = "Here is some text info";

      String[] infoWords = someTextInfo.split(" ");

      The resulting array in this case will have five elements in it, each storing a single word. The split method is capable of taking much more complex regular expressions indicating various arrangements and combinations of characters to match.

    New Line Characters

    • There are a few different types of new line character Java programs may encounter. Depending on the context in which a text file was created, it may contain new lines represented using the following two characters:

      \r

      \n

      The following syntax demonstrates using the string spit method to separate lines delineated by the two types of new line character used in plain text files, the "new line" and "carriage return" characters:

      String someTextLines = "First line\nSecond line\rAnother line";

      String[] lines = someText.split("[\r\n]+");

      This syntax will match any combination of the two character types, storing a single line at each array position.

    Return Array

    • The string split method in Java returns a string array for any further processing you may require. Programs can access the array elements directly as follows:

      System.out.println(someText.split("[\r\n]+")[0]);

      This code outputs the first line in the text string. Alternatively you can store the array as a variable, looping through it for processing as follows:

      String[] lines = someText.split("[\r\n]+");

      for(int i=0; i<lines.length; i++){

      System.out.println("Line "+i+": "+lines[i]);

      }

      This code outputs each line preceded by the line number.

Related Searches

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Related Ads

View Blog Post

Enter the Geek Vs Geek Giveaway and Win a Roku!