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 : 28/06/2009 20:53:57
Vue : 18414
Catégorie(s) : Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- VB 2005, 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
Date ajout : 04/03/2007
Posté par Charles Racaud [Liste]
Private Function MidStr(ByVal Str As StringByVal sStart As StringByVal sEnd As StringOptional ByVal Start As Integer = 30) As String
  Try
    Dim iStart As Integer = Str.IndexOf(sStart, Start) + 1
    Dim iEnd As Integer = Str.IndexOf(sEnd, iStart)
    Return Str.Substring(iStart, iEnd - iStart)
  Catch ex As Exception
    Return String.Empty
  End Try
End Function
Langage : C# 1.x , C# 2.x
Date ajout : 04/03/2007
Posté par Charles Racaud [Liste]
static string MidStr(string Str, string sStart, string sEnd, int Start) { 
  try {
    int iStart = Str.IndexOf(sStart, Start) + 1; 
    int iEnd = Str.IndexOf(sEnd, iStart);
    return Str.Substring(iStart, iEnd - iStart); 
  }
  catch {    return string.Empty; 
  }

}

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

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

{} TESTEUR DE COMMENTAIRE
Très utile dans les parseurs de code type vb ou javascript, il permet d'évaluer si une chaine est co...

{} TESTEUR ISBN
Cette regex permet de tester la validité d'une chaine de type ISBN (ex: identifiant pour les livres)...

{Visual Basic, VB6, VB.NET, VB 2005} ANTENNE YAGI - CONCEPTION
L'antenne Yagi est très utilisée pour la réception des chaines de télévision. Vous allez vous-même c...

{C / C++ / C++.NET} MYSTRING, CLASSE TRAITANT DES CHAÎNES DE CARACTÈRES
Ceci est une classe tout ce qu'il y'a de plus banal traitant des chaines de caractères. Commentée ai...