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 : 30161
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

{JAVA / J2EE} STRING NUMBERS COMPUTATOR
Cette classe permet simplement d'appliquer une fonction (simple ou complexe : de int -> vers int) à ...

{C / C++ / C++.NET} [C] WD_STRING V2.2
Fonctions de gestion des chaînes de caractères en langage C. Fonction Inverse : Renvoie pour chaq...

{C / C++ / C++.NET} [C] WD_STRING V1.9
Fonctions de gestion des chaînes de caractères en langage C. Fonction ChaineCompare : Compare deux ...

{Delphi} STREAM STRINGWRITER... UNE MICRO-LIBRAIRIE POUR ÉCRIRE DES CHAINES DANS UN FLUX
Cette micro-librairie (2 fonctions seulement) permet de lire et écrire des chaines de caractères dan...

{PHP} SÉCURISATION DE FORMULAIRE
Bonjour, j'ai lu sur divers forums que l'injection sql peut etre dangereux si on sécurise mal le tr...

{Visual Basic, VB6, VB.NET, VB 2005} CHAÎNE ALÉATOIRE / RANDOM STRING
Voila une petite fonction pour avoir une chaîne de caractère aléatoire composer de lettres(maj , min...

{C / C++ / C++.NET} GESTION DES NOTES D'UNE CLASSE
Cette application permet de gerer les notes d'une classe. On a utilisé la bibliotheque ncurses pour...

{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...