Sunday, September 28, 2014

How to Format a Parser in a Java String

Instructions

    1

    Type the following code:

    String myString = "This is the string I want to parse";

    This code creates a string variable named "myString" that you want to divide using your parser.

    2

    Type the following code:

    String delimiter = "[i]";

    This creates a string that you will use as your delimiter, which indicates the characters you want to use as tokens to break up the parsed string. In this example, you use only a lowercase "i." Uppercase "I" will not be included in the parsing process.

    3

    Type the following code:

    String[] myParsedString = myString.split(delimiter);

    This line parses the string, using the delimiter to break it up. The square brackets after "String" indicates you want to create an array to save the results. The value of "myParsedString" after execution is ("Th", "s ", "s the str", "ng I want to parse").

    4

    Type the following code:

    String[] myParsedString = myString.split(delimiter, 3);

    This line parses the string similarly to the previews example, except it limits the array result to three values, ignoring any occurrences of the delimiter after the third. The value of "myParsedString" after execution is ("Th", "s ", "s the string I want to parse").



No comments:

Post a Comment