Les Snippets

Connexion

Remplacer une chaîne (inconnue) placée entre deux chaînes (connues)

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 13/02/2007 23:12:50 et initié par PCPT [Liste]
Date de mise à jour : 29/07/2009 16:50:52
Vue : 11420
Catégorie(s) : Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- Windev
- Delphi 5
- Delphi 5
- VB 2005, VB 2008, VB.NET 1.x
- C# 1.x, C# 2.x



Langage : VB6 , VBA
Date ajout : 13/02/2007
Posté par PCPT [Liste]
Private Sub ReplaceString(ByRef Expression As String, sLeft As String, sRight As String, sRep As String)
    Dim lPosL As Long, lPosR As Long
    lPosL = InStr(1, Expression, sLeft): lPosR = InStr(lPosL + 1, Expression,  sRight)
    If lPosL > And lPosR > Then Expression = Left$(Expression, lPosL + Len(sLeft) - 1)  & sRep & Mid$(Expression, lPosR)
End Sub
'EXEMPLE  D'UTILISATION
Private Sub Form_Load()
    Dim sStr As String
    sStr = "<div  align='center'>machin</div>"
    Call ReplaceString(sStr, "'>""</""budule")
    MsgBox sStr
    Unload Me
End Sub

Remarque :
ici on veut remplacer "machin" sans le connaître, donc juste entre les balises. pour simplement supprimer il suffit d'indiquer une chaîne vide dans le paramètre sRep
Langage : Windev
Date ajout : 13/04/2007
Posté par Elian Lacroix [Liste]
sTexte est une chaîne = "remplacer le texte entre deux chaines qui servent de marqueurs"
sMarqueDébut est une chaîne = "texte"
sMarqueFin est une chaîne = "qui"
sNouveauTexte est une chaîne = "A LA PLACE"
Info(Remplace(sTexte, ExtraitChaîne(ExtraitChaîne(sTexte,2,sMarqueDébut),1,sMarqueFin), sNouveauTexte))

Langage : Delphi 5
Date ajout : 17/12/2007
Posté par japee [Liste]
DateMAJ : 17/02/2008
function ReplaceMidStr(const S, SLeft, SRight, NewStr: string): string;
var
  PosLeft, PosRight, PosMid: Integer;
  STmp: string;
begin
  Result := S;
  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;
  Delete(STmp, 1, PosRight - 1);
  Result := Copy(S, 1, PosMid - 1) + NewStr + STmp;
end;

Remarque :
Mise à jour le 17/02/2008.
Un grand merci à Cirec pour m'avoir 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 ... jusqu'à deux fois plus rapide} 
Uses StrUtils; {Indispensable pour PosEx} 
Function ReplaceMidStr(Const  S, SLeft, SRight, NewStr: 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, 1, PosLeft-1) + NewStr + Copy(S, PosRight, High(Integer)); 
End; 
 
 
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 29/07/2009
Posté par PCPT [Liste]
DateMAJ : 29/07/2009
    Private Sub ReplaceString(ByRef Expression As StringByVal sLeft As StringByVal sRight As StringByVal sReplace As StringOptional ByVal iStart As Integer = 0)
        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
                Expression = String.Format("{0}{1}{2}", Expression.Substring(0, iPosL + sLeft.Length), sReplace, Expression.Substring(iPosR, Expression.Length - iPosR))
            End If
        End If
    End Sub

'Dim sStr As String = "<div align='center'>machin</div>"
'ReplaceString(sStr, "'>", "<", "budule")
'MessageBox.Show(sStr)

Langage : C# 1.x , C# 2.x
Date ajout : 29/09/2009
Posté par Willi [Liste]
private void ReplaceString(ref string Expression, string sLeft, string sRight, string sReplace,  int iStart)
{
  int iPosL = Expression.IndexOf(sLeft, iStart);
  if (iPosL > -1)
  {
    int iPosR = Expression.IndexOf(sRight, iPosL + sLeft.Length);
    if (iPosR > -1)
    {
      Expression = string.Format("{0}{1}{2}", Expression.Substring(0, iPosL + sLeft.Length), sReplace, Expression.Substring(iPosR, Expression.Length - iPosR));
    }
  }
}


Snippets en rapport avec : Chaine, Remplacer, Inconnue



Codes sources en rapport avec : Chaine, Remplacer, Inconnue

{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} RACCOURCIR UNE CHAINE AVEC UN SÉPARATEUR AU MILIEU
Vous avec une chaine de ce type : J'aime le PHP et j'espère que ma modeste source vous plaira ...

{Visual Basic, VB6, VB.NET, VB 2005} WINSED (RECHERCHER / REMPLACER)
WinSed permet de rechercher et de remplacer une chaine dans plusieurs fichiers....

{ASP / ASP.NET} TROUVER UNE CHAINE ET LA REMPLACER
Trouve str1 dans la chaine s et le remplace par str2 inputs : s, str1, str2 au format string ...

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

{PHP} REMPLACER DES LETTRES EN IMAGES
C'est un ancien code que j'avais fait juste dans un but esthétique, son but est tout simplement de r...

{C# / C#.NET} REMPLACER UNE SÉQUENCE DE BYTE
Cette méthode sert à remplacer simultanément dans une séquence byte[] A toutes les sous-séquences by...

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