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 : 17/02/2008 14:56:16
Vue : 11168
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]
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
'EXEMPLE  D'UTILISATION
Private Sub Form_Load()
    Dim sStr As String
    MsgBox MyMid("<div  align='center'>machin</div>""'>""</")
    Unload Me
End Sub

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

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

{C / C++ / C++.NET} CHAINE EN HEXA (WIN32)
Pour question récurrente sur forum. char* __fastcall bnStrToHex(char *szsrc, char* szdst); retou...

{PHP} OPTIMISATION DE CHAINE POUR RÉÉCRITURE D'URL
Ce code est une petite fonction qui permet d'optimisé une chaine de caractére pour la placer dans un...

{Delphi} DELPHI : PROCEDURE SPLIT SIMILAIRE A LA FONCTION EPONYME EN VB
Découpe une chaîne délimitée en ses éléments pour remplir un tableau dynamique passé en paramètre. U...

{PHP} RENVOI UN DOSSIER DE NOM UNIQUE ET ALÉATOIRE
Petite fonction permettant de renvoyer un dossier disponible de nom aléatoire dans le chemin précisé...

{PHP} CLASSE DE COUPAGE DE TEXTE HTML AVEC CONSERVATION DES BALISES
Bonjour, Cette classe permet de couper une chaîne de caractère à une longueur donnée. La chaîne s...

{Visual Basic, VB6, VB.NET, VB 2005} PROGRAMME POUR RENOMMER PLUSIEUR FICHIER
J'ai fait ce programme pour renommer les fichiers (a l'origine les mp3). Il permet d'enlever , rajo...

{Visual Basic, VB6, VB.NET, VB 2005} EVALUER/EXECUTER UNE CHAINE EN TANT QUE CODE
Cette source vous permettra d'exécuter une chaîne contenant du code source en VB.Net. J'utilise Cod...

{Javascript / DHTML} REMPLACER UN MOT DANS UNE CHAINE
Bonjour, N'aimant pas travailler avec les expressions régulières sous javascript, voici une fonct...

{PHP} FILTRE À GROS MOTS POUR LIVRE D'OR
J'ai 54 ans et c'est mon premier post, j'en tremble encore... C'est une fonction toute bête qui fil...