Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: String Regex Methods

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(regext) 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);
}
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.