Aide java

saf20201020 Messages postés 3 Date d'inscription   Statut Membre Dernière intervention   -  
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,
c'est quoi la methode en jave qui a le meme sens que strstr(en c) ????


A voir également:

2 réponses

choubaka Messages postés 39442 Date d'inscription   Statut Modérateur Dernière intervention   2 105
 
Bonjour

ça doit être ceci

startsWith  

public boolean startsWith(String prefix)  

    Tests if this string starts with the specified prefix.  

    Parameters:  
        prefix - the prefix.   
    Returns:  
        true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.  
    Since:  
        1. 0  


Chouba, Modo CCM
Rhâââgnagna
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
const char * strstr ( const char * str1, const char * str2 );
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

En Java, ça n'a pas vraiment de sens de faire pareil, car on ne manipule pas de pointeurs, mais des références sur des objets, or les String sont immuables, ils ne peuvent pas être modifiés ou décomposés, en gros, peu importe quelles méthodes tu utiliseras, tu vas créer un nouvel objet String.

On pourrait donc faire ceci, pour se rapprocher le plus du comportement de strstr, mais l'intérêt est du coup assez limité en Java :
String str = str1. indexOf(str2)>0 ? new String(str2) : null;
La confiance n'exclut pas le contrôle
0