Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

String Regex Methods

JavaFAQ Home » Text processing Go to all tips in Text processing


Bookmark and Share

In addition the the Pattern and Matcher classes, there is some support for regular expressions in the String class.

    boolean b;
    String s, s2, t;
    String  regex;
    String[]  sa;
    
b = s.matches(regex) True if regex matches the entire string in s. Same as Pattern.matches(regex, s)
s2 = s.replaceAll(regex, t) Returns a new string by replacing each substring in s that matches regex with t
s2 = s.replaceFirst(regex, t) Replaces first substring in s that is matched by regex with String t
sa = s.split(regex) Breaks s into substrings separated by regex or terminated by the end. There is a problem with this though. If the string is terminated by the separation regex, it will return one extra empty string. If you're separating elements with whitespace ("s+"), you can call trim() on the subject first. See the example below.
sa = s.split(regex, count) Splits the string, but limits applying regex to only count times.

Example - breaking strings into parts

Input lines often contain many values. An easy way to split a string into separate parts is to use the String split() method. This example splits the line into tokes that are separated by one or more blanks.

// Example of splitting lines from input file with regular expression.
while ((line = input.readLine()) != null) {
    String[] tokens = line.trim().split("s+"); // Separated by "whitespace"
    int sum = 0;
    for (String t : tokens) {
        try {
            sum += Integer.parseInt(t);
        } catch (NumberFormatException ex) {
            System.out.println("Not an integer: " + t);
            System.out.println(ex);
        }
    }
    System.out.println("sum = " + sum);
}

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy