Les Snippets

Connexion

Récupérer une chaîne (inconnue) placée entre deux chaînes (connues)

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 04/03/2007 18:52:00 et initié par PCPT [Liste]
Date de mise à jour : 29/07/2009 20:32:31
Vue : 21801
Catégorie(s) : Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- VB 2005, VB 2008, VB.NET 1.x
- C# 1.x, C# 2.x
- PHP 3, PHP 4, PHP 5
- Windev
- Delphi 5
- Delphi 5



Langage : VB6 , VBA
Date ajout : 04/03/2007
Posté par PCPT [Liste]
DateMAJ : 28/06/2009
Private Function MyMid(ByRef Expression As String, sLeft As String, sRight As String, Optional Start As Long = 1) As  String
    Dim lPosL As Long, lPosR As Long
    lPosL = InStr(Start, Expression, sLeft): lPosR = InStr(lPosL + 1, Expression,  sRight)
    If lPosL > And lPosR > Then
        MyMid = Mid$(Expression, lPosL + Len(sLeft), lPosR - lPosL - Len(sLeft))
    Else
        MyMid = vbNullString
    End If
End Function

Remarque :
MsgBox MyMid("machin", "'>", "
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 04/03/2007
Posté par Charles Racaud [Liste]
DateMAJ : 29/07/2009
    Private Function MidStr(ByVal Expression As StringByVal sLeft As StringByVal sRight As StringOptional ByVal iStart As Integer = 0As String
        Dim iPosL As Integer = Expression.IndexOf(sLeft, iStart)
        If iPosL > -1 Then
            Dim iPosR As Integer = Expression.IndexOf(sRight, iPosL + sLeft.Length)
            If iPosR = -1 Then
                'pas le caractère de fin, on prend la chaîne complète
                iPosR = Expression.Length - iPosL - sLeft.Length
                sRight = String.Empty
            End If
            Return Expression.Substring(iPosL + sLeft.Length, iPosR - iPosL - sLeft.Length)
        End If
        Return String.Empty
    End Function
Langage : C# 1.x , C# 2.x
Date ajout : 04/03/2007
Posté par Charles Racaud [Liste]
DateMAJ : 29/07/2009
static string MidStr(string Str, string sStart, string sEnd) { 
  return MidStr(Str, sStart, sEnd, 0); 
} 
static string MidStr(string Str, string sStart, string sEnd, int Start) { 
  if (!string.IsNullOrEmpty(Str)) { 
    int iStart = Str.IndexOf(sStart, Start); 
    if (iStart > -1) { 
      int sStartLength = sStart.Length; 
      if (string.IsNullOrEmpty(sEnd)) 
        return Str.Substring(iStart + sStartLength); 
      else { 
        int iEnd = Str.IndexOf(sEnd, iStart + sStartLength); 
        if (iEnd > -1) 
          return Str.Substring(iStart + sStartLength, iEnd - iStart - sStartLength); 
      } 
    } 
  } 
  return string.Empty; 
}
Remarque :
Si Str vide -> retourne une chaîne vide.
Si sStart vide -> retourne du début jusqu'à sEnd.
Si sEnd vide -> retourne de sStart à la fin.
Si sStart ou sEnd non trouvé -> retourne une chaîne vide.
Langage : PHP 3 , PHP 4 , PHP 5
Date ajout : 29/03/2007
Posté par pifou25 [Liste]
function extractBetweenDelimeters($inputstr,$delimeterLeft,$delimeterRight) {
    $posLeft  = stripos($inputstr,$delimeterLeft)+strlen($delimeterLeft);
    $posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
    return  substr($inputstr,$posLeft,$posRight-$posLeft);
 }
 
Remarque :
copyright depuis un commentaire chez http://fr.php.net/substr
Langage : Windev
Date ajout : 13/04/2007
Posté par Elian Lacroix [Liste]
sTexte est une chaîne = "récupérer le texte entre deux chaines qui servent de marqueurs"
sMarqueDébut est une chaîne = "texte"
sMarqueFin est une chaîne = "qui"
Info(ExtraitChaîne(ExtraitChaîne(sTexte,2,sMarqueDébut),1,sMarqueFin))
Langage : Delphi 5
Date ajout : 17/12/2007
Posté par japee [Liste]
DateMAJ : 17/02/2008
function MidStr(const S, SLeft, SRight: string): string;
var
  PosLeft, PosRight, PosMid: Integer;
  STmp: string;
begin
  Result := '';
  PosLeft := Pos(SLeft, S);
  if PosLeft = 0 then Exit;
  PosMid := PosLeft + Length(SLeft);
  STmp := Copy(S, PosMid, Length(S));
  PosRight := Pos(SRight, STmp);
  if PosRight = 0 then Exit;
  Result := Copy(STmp, 1, PosRight - 1);
end;

Remarque :
Mise à jour le 17/02/2008
Un grand merci à Cirec qui m'a signalé un bug sur la version précédente.
Langage : Delphi 5
Date ajout : 17/02/2008
Posté par cirec [Liste]

{Alternative à la fonction de Japee ... de 1,33 à 1,75  fois plus rapide} 
Uses  StrUtils; {Indispensable pour  PosEx} 
Function  MidStr(Const S, SLeft, SRight:  String): String; 
Var  
  PosLeft, PosRight: Integer; 
Begin  
  PosLeft := Pos(SLeft, S); 
  If  PosLeft < 1 Then  Exit; 
  Inc(PosLeft, Length(SLeft)); 
  PosRight :=  PosEx(SRight, S, PosLeft); 
  If PosRight  < 1 Then Exit;  
  Result := Copy(S, PosLeft, PosRight - PosLeft); 
End; 
 
 

Snippets en rapport avec : Chaine, Inconnue, Mid



Codes sources en rapport avec : Chaine, Inconnue, Mid

{C / C++ / C++.NET} FONCTION : CHAR * AJUSTERTAILLECHAINE()
Cette fonction permet de retirer une partie de chaine de caractère ou d'espacer la chaine a partir d...

{JAVA / J2EE} NOMBRE D'OCCURENCE D'UNE CHAINE DE CARACTÈRE DANS TOUS LES FICHIERS AVEC LES ENTÊTES SPÉCIFIÉS CONTENU DANS UN DOSSIER
Le titre est assez explicite. Il s'agit d'un petit bout de code renvoyant le nombre de fois qu'une c...

{C# / C#.NET} GESTION DES LANGUES, COUNTRIES, CHAÎNES DE CARACTÈRE SIMPLE
Gérer les langues dans un programme peut devenir vite fastidieux. Vous pouvez utilisez le gestionnai...

{C / C++ / C++.NET} GSTRING - GESTION DES CHAINES DE CARACTÈRES
Voici une petite classe permettant de gérer les chaines de caractères tout comme les std::string, av...

{Assembleur} LECTURE ET ECRITURE D'UN CARACTERE ET D'UNE CHAINE DE CARACTERE !
bon j'ai essayer d'utiliser des interruption pour la première fois je suis vraiment débutant en ass...

{JAVA / J2EE} GENERATEUR DE MOT PAR INCREMENTATION
Cette class permet de renvoyer un mot à partir d'un autre avec une permutation du dernier caractère ...

{Delphi} LIBRAIRIE DE FONCTIONS DE GESTION DE SOUS-CHAINES.
Quelques fois, on aimerai avoir une sorte de "structure" de plusieurs éléments variable en taille et...

{Visual Basic, VB6, VB.NET, VB 2005} GESTION DE DONNÉES AVEC ACCESS
Cette source est un petit exemple qui a pour rôle de gérer les fonctions les plus utilisable en Base...

{} VALIDATEUR HEXADECIMAL (TYPE C)
Permet de tester la validité d'une chaine representant un nombre hexadecimal de type C ou javascript...

{} VALIDATEUR BINAIRE
Cette regex plutôt simple permet de valider une chaine representant un binaire, suite de zero et de ...