Aide Fonction StringInStr

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Raf
Niveau 1
Niveau 1
Messages : 2
Enregistré le : mer. 02 mars 2022 22:08
Status : Hors ligne

Aide Fonction StringInStr

#1

Message par Raf »

Bonjour,

j’aimerai une clarification sur la fonction StringInStr. Vu la doc, en cas au la chaîne n'est pas trouvé, on doit avoir un résultat à 0.
Valeur de retour
Succès: Retourne la position de la sous-chaîne.
Échec: Retourne 0 si la sous-chaîne n'est pas trouvée.
@error: 1 = Paramètres 'start' ou 'occurrence' invalide.

Du coup dans mon bout de code, je comprends pas pourquoi la valeur de $string est toujours égale à 0.
Au cas où la chaîne n’est pas trouvé je souhaite sortir du programme, pas seulement de la boucle.
On peut sûrement faire plus simple, je suis ouvert à toutes suggestions :D
local $file = @ScriptDir&"\ConsultaSaldos.txt"
local $recup = @ScriptDir&"\super.txt"
local $mot = "china"
local $i=1
While 1
   $line = FileReadLine($file,$i)
   If @error = -1 Then ExitLoop
   $string = StringInStr($line, $mot)
   If StringInStr($line, $mot) Then FileWriteLine($recup, $line)
   If $string = 0 Then Exit
   $i=$i+1
Wend
Merci de votre aide!
Avatar du membre
walkson
Modérateur
Modérateur
Messages : 1020
Enregistré le : ven. 12 août 2011 19:49
Localisation : Hurepoix
Status : Hors ligne

Re: Aide Fonction StringInStr

#2

Message par walkson »

Bonjour,

Code : Tout sélectionner

$text = "The Warring States period ended in 221 BCE after the state of Qin conquered the other six kingdoms," & @CRLF
$text &= "reunited China and established the dominant order of autocracy." & @CRLF
$text &= "King Zheng of Qin proclaimed himself the First Emperor of the Qin dynasty." & @CRLF
$text &= "He enacted Qin's legalist reforms throughout China, notably the forced standardization of Chinese characters," & @CRLF
$text &= "measurements, road widths (i.e., cart axles' length), and currency. His dynasty also conquered the Yue tribes in Guangxi," & @CRLF
$text &= "Guangdong, and Vietnam.[54] The Qin dynasty lasted only fifteen years, falling soon after the First Emperor's death," & @CRLF
$text &= "as his harsh authoritarian policies led to widespread rebellion."
;==========================================================================================================================================
local $file = @ScriptDir&"\ConsultaSaldos.txt"
FileWrite($file,$text)
local $recup = @ScriptDir&"\super.txt"
local $mot = "china"
local $i=1, $y = 0
While 1
   $line = FileReadLine($file,$i)
   If @error = -1 Then Exit ;fin du texte on sort
   If StringInStr($line, $mot) >= 1 Then
	   FileWriteLine($recup, $line)
	   $y += 1
   EndIf
;~ 	If $y = 1 Then Exit ;on quitte à la première occurence dans le texte
   $i += 1
Wend
Cordialement,
Walkson
"Horas non numero nisi serenas " Le canon de midi
(Je ne compte que les heures heureuses)
Avatar du membre
mikell
Spammer !
Spammer !
Messages : 6292
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: Aide Fonction StringInStr

#3

Message par mikell »

Pour info, il y a d'autres méthodes

$text = "The Warring States period ended in 221 BCE after the state of Qin conquered the other six kingdoms," & @CRLF
$text &= "reunited China and established the dominant order of autocracy." & @CRLF
$text &= "King Zheng of Qin proclaimed himself the First Emperor of the Qin dynasty." & @CRLF
$text &= "He enacted Qin's legalist reforms throughout China, notably the forced standardization of Chinese characters," & @CRLF
$text &= "measurements, road widths (i.e., cart axles' length), and currency. His dynasty also conquered the Yue tribes in Guangxi," & @CRLF
$text &= "Guangdong, and Vietnam.[54] The Qin dynasty lasted only fifteen years, falling soon after the First Emperor's death," & @CRLF
$text &= "as his harsh authoritarian policies led to widespread rebellion."
;=============================================================================

; #Include <Array.au3>
#include <File.au3>

; local $file = @ScriptDir&"\ConsultaSaldos.txt"
; FileWrite($file, $text)
; $text = FileRead($file)
local $recup = @ScriptDir&"\super.txt"
local $mot = "china"

$a_recup = StringRegExp($text, '(?im)^.*?\Q' & $mot & '\E.*', 3)
If IsArray($a_recup) Then
   ; _ArrayDisplay($a_recup)
   $h = FileOpen($recup, 1)
   _FileWriteFromArray($h, $a_recup)
   FileClose($h)
EndIf
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Raf
Niveau 1
Niveau 1
Messages : 2
Enregistré le : mer. 02 mars 2022 22:08
Status : Hors ligne

Re: Aide Fonction StringInStr  

#4

Message par Raf »

Merci pour vos conseils.
Je vais creuser la question !
Répondre