Chercher et extraire un caractere dans une string

cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007 - 6 avril 2006 à 19:50
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007 - 7 avril 2006 à 14:54
En c#.net (vs 2005) j'aimerais rechercher un caractere dans une string, et ensuite l'extraire pour le mettre dans une autre variable.
J'ai essayé ceci mais ca n'a rien avoir avec le c#, ca correspond plus au C..
/*for (int
i=0;i>res.Length;i++)



{



if (res[i]=="$")



{



int tempa = Int16.Parse(res[i+1]);



tempa = tempa*10;



tempa = tempa + Int16.Parse(res[i+2]);



texttempa.Text= tempa.ToString;



}



}*/

Merci d'avance pour le coup de main

15 réponses

cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
6 avril 2006 à 20:06
Salut,
Ca ne change pas beaucoup, le code ci-dessus traduit en C# donne :



for (
int i = 0; i > res.Length; i++)
{

if (res[i] ==
'$')
{

int tempa = (
int)(res[i + 1]);
tempa = tempa * 10;
tempa = tempa + (
int)(res[i + 2]);
texttempa.Text = tempa.ToString();
}
}


<HR>
[/auteurdetail.aspx?ID=13319
[Pub] ]http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319
0
huberdine Messages postés 158 Date d'inscription samedi 8 février 2003 Statut Membre Dernière intervention 17 juin 2010
6 avril 2006 à 21:58
ça marchera mieux comme ça ;-)
for (
int i = 0; i < res.Length; i++)
{

if (res[i] ==
'$')
{

int tempa = (
int)(res[i + 1]);
tempa = tempa * 10;
tempa = tempa + (
int)(res[i + 2]);
texttempa.Text = tempa.ToString();
}
}

huberdine, mais dort aussi ...
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 08:43
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 11:29
Ca ne marche pas trop : il me renvois la valeur en ascii du caractere decimal que je voudrais transformer en int

Merci pour l'aide !
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 11:37
string res =
"test$fin";

for (
int i = 0; i < res.Length; i++)
{

if (res[i] ==
'$')
{

int tempa = (
int)(res[i + 1]);
tempa = tempa * 10 + (
int)(res[i + 2]);
texttempa.Text = ((
char)tempa).ToString();
}
}

Mais quel est le but en fait ?


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 11:41
En fait c'est un programme qui va regarder le code source d'un serveur web embarqué dans un groupe electrogene. DAns ce code il y a 2 temperatures moteurs. Elles sont l'une apres un "$" et l'autre apres un "#". Donc j'aimerais les recuperer pour ensuite en faire un graphique !

Et si je faisais -49 ? c'est moin propre evidement...
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 11:47
plutot -48 d'apres une table de code ascii :p
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 11:50
A toi de prendre en compte toutes les exceptions qui peuvent être levées :

private
void Form1_Load(
object sender,
EventArgs e)
{

int val =
this.GetValue(
"WDKJXLKSMAslkmdl$23josad903");

Console.WriteLine(val);
}



private
int GetValue(
string str)
{

int index = str.IndexOf(
'$');

return
Int32.Parse(str.Substring(index + 1, 2));
}


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 12:37
On peut faire un peu plus complet avec les expressions régulières :

using System.Text.RegularExpressions;



private
void Form1_Load(
object sender,
EventArgs e)
{

int[] val1 =
this.GetValues(
"WDKJXLKSMAs#34lkmdl$23josa#54d903");

// val 1 contient 34, 23 et 54
}



private
int[] GetValues(
string str)
{

MatchCollection mc =
Regex.Matches(str,
@"(\$|#)(?<val>\d+)",
RegexOptions.IgnoreCase);

int[] val =
new
int[mc.Count];

for (int i 0; i < mc.Count; i++) val[i]
Int32.Parse(mc[i].Groups[
"val"].Value);

return val;
}


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 12:59
Ouaip cool merci.

Je vais encore t'embeter Dans le cas de valeurs avec virgule, il suffit de remplacer les int par des float ?

J'avais trouvé ca comme solution (bien moin propre evidement )

for (int i = 0; i < res.Length; i++)
{
if (res[i] == '$')
{
float tempa = (float)(res[i + 1]);
tempa = tempa-48;
tempa = tempa * 10;

float tempb = (float)(res[i + 2]);
tempb = tempb-48;
tempa=tempa+tempb;

float tempc = (float)(res[i+4]);
tempc = tempc-48;
tempc = tempc/10;
float temp11= tempa+tempc;

TBtempa.Text = tempa.ToString();
}
if (res[i] == '#')
{
float tempa = (float)(res[i + 1]);
tempa = tempa-48;
tempa = tempa * 10;

float tempb = (float)(res[i + 2]);
tempb = tempb-48;
tempa=tempa+tempb;

float tempc = (float)(res[i+4]);
tempc = tempc-48;
tempc = tempc/10;
float temp12= tempa+tempc;

TBtempb.Text = tempa.ToString();
}
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 13:01
Voila un c/c du code source pour que tu vois de quel genre il est :

HTTP/1.0 200 OK
Content-Type: text/html
Content-Length: 195

<html>Temp int:$21,5C

Temp ext:#22,0C

Conso :&17.81L/Hr
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 13:29
private
void Form1_Load(
object sender,
EventArgs e)
{

double[] val1 =
this.GetValues(
@"$21.5.3C#22.0C

Conso :&17.81L/Hr

");
}



private
double[] GetValues(
string str)
{

MatchCollection mc =
Regex.Matches(str,
@"(\$|#)(?<val>(\d+\.?\d+))",
RegexOptions.IgnoreCase);

double[] val =
new
double[mc.Count];

for (int i 0; i < mc.Count; i++) val[i]
Double.Parse(mc[i].Groups[
"val"].Value);

return val;
}


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 13:33
Si les nombres sont écrits à virgule et pas avoir des points (4,3 => 4.3) alors on a ceci :


private
void Form1_Load(
object sender,
EventArgs e)
{

double[] val1 =
this.GetValues(
@"$21,53C#22,0C

Conso :&17.81L/Hr

");
}



private
double[] GetValues(
string str)
{

MatchCollection mc =
Regex.Matches(str,
@"(\$|#)(?<val>(\d+\,?\d+))",
RegexOptions.IgnoreCase);

double[] val =
new
double[mc.Count];

for (int i 0; i < mc.Count; i++) val[i]
Double.Parse(mc[i].Groups[
"val"].Value);

return val;
}

Il peut éventuellement y avoir une erreur sur la ligne Double.Parse(mc[i].Groups["val"].Value);
Dans ce cas, il faut faire le parsing en spécifiant la culture à utiliser.


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
7 avril 2006 à 13:55
Donc pour finir, on aurait quelque chose comme ceci :
(en tenant compte de la culture cette fois-ci)



private
void Form1_Load(
object sender,
EventArgs e)
{

double[] val =
this.GetValues(
@"$21,5.3C#22,0C");
}



private
double[] GetValues(
string str)
{

// Create a new CultureInfo
CultureInfo c =
new
CultureInfo(
CultureInfo.CurrentCulture.Name);
c.NumberFormat.NumberDecimalSeparator =
",";

// Regex
MatchCollection mc =
Regex.Matches(str,
@"(\$|#)(?<val>(\d+\,?\d+))",
RegexOptions.IgnoreCase);

double[] val =
new
double[mc.Count];

for (int i 0; i < mc.Count; i++) val[i]
Double.Parse(mc[i].Groups[
"val"].Value, c);

return val;
}


<HR>
/auteurdetail.aspx?ID=13319[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever /infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319/infomsg/auteurdetail.aspx?ID=13319
0
cedoli Messages postés 58 Date d'inscription mercredi 25 janvier 2006 Statut Membre Dernière intervention 13 avril 2007
7 avril 2006 à 14:54
ouaip ok je travaille ca :p je te tient au courant, merci pour le mega coup de main
0
Rejoignez-nous