e-Babillard - plateforme pédagogique interactive

Ressources pédagogiques communes concernant... => JAVA => Sujet démarré par: jpdelx sur 17 Novembre 2010 à 18:12:14



Titre: [JAVA] Classe String - méthode matches
Posté par: jpdelx sur 17 Novembre 2010 à 18:12:14
Bonjour
Citation:
...La classe « java.lang.String » en Java contient déjà un support assez avancé pour les expressions régulières.
On peut ainsi vérifier la concordance d’un motif, séparer une chaîne de caractères en sous-chaînes en utilisant
des motifs et, finalement, utiliser la fonction rechercher/remplacer...

[ source (http://benhur.teluq.uqam.ca/SPIP/inf6460/article.php3?id_article=29&id_rubrique=14) ]

~> Class String [ en savoir plus... (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html) ] et Method matches (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#matches%28java.lang.String%29)

Java String matches Example [ source (http://www.codingdiary.com/developers/developers/diary/javaapi/java/lang/SampleCode/MatchesStringExampleCode.html) ]
Citation:
String class matches method example:- This example demonstrates the working of matches() method. this method returns boolean values if it gets same match of object value to the passes argument value in the parentheses.

Code:
/**
* @(#) MatchesString.java
* MatchesString class demonstrates the working of matches() method of String class of lang package
* @version 16-May-2008
* @author Rose India Team
*/


public class MatchesString {
  public static void main(String args[]) {

    // method just matches the String value to the specified value in the
    // parentheses and generates boolean values
    String str = " heram", str1 = "xaaaaa";
    boolean result = str1.matches("aaaaa");
    // method not get the same values therefore it returns false
    System.out.println("Method returns 'false':  " + result);

    // method gets the same values but not able to match there allignments
    // therefore it returns false
    result = str.matches("heram");
    System.out.println("Method returns 'false':  " + result);

    // method gets the same values with precise allignments therefore it
    // returns true
    result = str.matches(" heram");
    System.out.println("Method returns 'true':  " + result);
  }
}

@lpf


e-Babillard - plateforme pédagogique interactive | Propulsé par SMF 1.0.6 modifié par jpdelx.
© 2001-2005, Lewis Media. Tous droits réservés.