Instr en c# ne Fonctionne pas

Résolu
danmor Messages postés 31 Statut Membre -  
danmor Messages postés 31 Statut Membre -
Bonjour,

si une personne peut m'aider serais aprécié

Jutilise ceci en vb.net mais ne fontionee pas en c# ca je sais a cause ce instr()

voici le probleme en entier

Public Sub WriteDate(ByVal start_at As Integer)
Dim pos As Integer
Dim target As String
target = Format(DateString, "Long date")
pos = InStr(start_at, Rtxt.Text, target)
If pos <> 0 Then
TargetPosition = pos
Rtxt.SelectionStart = TargetPosition - 1
Rtxt.SelectionStart = Len(Rtxt.Text)
Rtxt.Modified = False
Rtxt.Focus()
Exit Sub
Else
Rtxt.SelectionStart = Len(Rtxt.Text)
Rtxt.SelectedText = vbCrLf
Rtxt.SelectedText = vbCrLf
Rtxt.SelectionFont = New Font(Font.Name, 10, FontStyle.Bold)
Rtxt.SelectedText = Format(DateString, "Long date")
Rtxt.SelectionFont = New Font(Font.Name, Font.Size, FontStyle.Regular)
Rtxt.SelectedText = vbCrLf
Rtxt.SelectedText = vbCrLf
Rtxt.SelectionStart = Len(Rtxt.Text)
Rtxt.Focus()
End If
End Sub

Jai refait en c# de cette facon

public void WriteDate(int start_at)
{
int pos;
string target;
target = DateTime.Now.ToString("dddd, dd MMM yyyy");
pos = InStr(start_at, Rtxt.Text, target); // Le probleme est ici
if (pos != 0)
{
TargetPosition = pos;
Rtxt.SelectionStart = TargetPosition - 1;
Rtxt.SelectionStart = Rtxt.TextLength;
Rtxt.Modified = false;
Rtxt.Focus();
return;
}
else
{
Rtxt.SelectionStart = Rtxt.TextLength;
Rtxt.SelectedText ="\r\n";
Rtxt.SelectedText = "\r\n";
Rtxt.SelectionFont = new Font(Font.Name, 10, FontStyle.Bold);
Rtxt.SelectedText = DateTime.Now.ToString("dddd, dd MMM yyyy");
Rtxt.SelectionFont = new Font(Font.Name, Font.Size, FontStyle.Regular);
Rtxt.SelectedText ="\r\n";;
Rtxt.SelectedText ="\r\n";;
Rtxt.SelectionStart = Rtxt.TextLength;
Rtxt.Focus();
}
}

vc# 2008
Merci de votre aide

DanMor
Configuration: Windows XP
Internet Explorer 7.0

2 réponses

  1. amigo
     
    Bonjour,

    Excuses-moi, je pensais qu'on pouvait facilement transposer.

    Voici la fonction Inst en C#

    
    /****************************************************************
     * Fonction int Instr(chaine,sous_chaine)
     * exemples d'utilisation
     ****************************************************************/ 
    using System;
    
    class Test {
      static void Main() {
    int i;
    
    string txt1="0123456789";
    string txt2 ="01234567891";
    
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Chaine = ",txt1));
    Console.WriteLine(String.Concat("Sous-Chaine = ", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche -1*/
    
    txt2="0";
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Sous-Chaine = ", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche 0*/
    
    txt2="9";
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Sous-Chaine =", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche 9*/
    
    txt2="567";
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Sous-Chaine = ", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche 5*/
    
    
    txt2="";
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Sous-Chaine = ", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche -1*/
    
    txt1="A ZER TYUIOPA ZET YUI ZET OP";
    txt2="ZET";
    i=Test.Instr(txt1,txt2);
    Console.WriteLine(String.Concat("Chaine = ",txt1));
    Console.WriteLine(String.Concat("Sous-Chaine = ", txt2));
    Console.Write("Position de la Sous-Chaine : ");
    Console.WriteLine(i);/*affiche 14*/
    
    txt1=Console.ReadLine();
    
        }
    	
    /****************************************************************
     * Fonction int Instr(chaine,sous_chaine)
     * si chaine=""  ou sous_chaine="" retourne -1
     * si length(sous_chaine) > length(chaine) retourne -1
     * 
     * si sous_chaine est dans chaine retourne position 1er caractère
     * a partir de 0
     * sinon retourne -1
     * 
     * exemples d'utilisation
     ****************************************************************/ 
    static int Instr(string chaine , string sous_chaine)
    {
       int l = chaine.Length;
       int m = sous_chaine.Length;
       int n = -1;
       int i; 
       int j; 
       char c1;
       char c2; 
       if ((m > l) || (m == 0) || (l == 0)) return n;
    
       j = 0;
       while (j<l)
       { 
       i=0; c2=sous_chaine[0];
       c1=chaine[j];
         if (c1==c2) {
           while ((i<m) && (j<l))
           {
             i = i+1; j = j+1;
             if ((j<l) && (i<m))
             {c1=chaine[j]; c2=sous_chaine[i];}
             if (c1!=c2) break;
           }
        }
          if ( i == m)
             {
              n = j-m;
              return n;
             }     
      j=j+1; 
      }
    return n;
    }
    /****************************************************************/
    	
      }
    
    1
    1. danmor Messages postés 31 Statut Membre
       
      Merci AMIGO c'est bon ca tres vite comme reponse

      encore merci

      danmor
      0
  2. amigo
     
    Bonjour,

    Voici l'equivalent de la fonction instr en C avec un exemple d'utilisation
    contrairement a VB la numérotaion des caractères commence à 0 et non à 1.
    #include <stdio.h>
    #include <stdlib.h>
    
    int instr(); 
    
    int main(){
    int i;
    char txt[11];
    
    strcpy(txt,"0123456789"); 
    i=instr(txt,"5");
    printf("position de la sous-chaine %i \n",i);/*affiche 5*/
    
    strcpy(txt,"AZERTYUIOP");
    i=instr(txt,"x");
    printf("position de la sous-chaine %i \n",i);/*affiche -1*/
    
    getch();
    return 0;
    }
    
    
    /*si sous_chaine est dans chaine,
      retourne position 1er caratère de sous_chaine a partir de 0,
      sinon retourne -1*/
    
    int instr (chaine , sous_chaine)
    char *chaine;
    char *sous_chaine;
    {
       int l , m , n ,i ,j ;
       l = strlen(chaine);
       m = strlen(sous_chaine);
       n = -1;
       if ((m > l) || (m == 0) || (l == 0)) return(n);
       j = -1;
       while (j<l)
       { 
        i = 0; j = j+1; 
        while ((sous_chaine[i]==chaine[j]) && (i<m) && (j<l))
          {
            i = i+1; j = j+1;
          }
          if ( i == m)
             {
              n = j-m;
              return(n);
             }
       }
    return(n);
    }
    
    0
    1. danmor Messages postés 31 Statut Membre
       
      Merci amigo mais ca maide pas car tu me la envoyer en c ++ et non en c#
      0