Hello,
I créates several Functions because AGK2 does not have them:
CountString
FindString
ReverseString
InsertString
RemoveString
ReplaceString
CopyString
CutString
Probably do little better now but already it's not so bad, I hope clenched added to AGK2 later
Function CountString(String$, StringToCount$, ModeCase)
Word$ = ""
CountWord = 0
If ModeCase < 0
ModeCase = 0
ElseIf ModeCase > 1
ModeCase = 1
EndIf
If ModeCase = 0
String$ = Lower(String$)
StringToFind$ = Lower(StringToFind$)
EndIf
For Index = 1 To Len(String$) Step Len(StringToCount$)
Word$ = Mid(String$, Index, Len(StringToCount$))
If Word$ = StringToCount$
CountWord = CountWord + 1
EndIf
Next
EndFunction CountWord
Function FindString(String$, StringToFind$, StartPosition, ModeCase)
FindWord = 0
Word$ = ""
If StartPosition = 0
StartPosition = 1
ElseIf StartPosition = -1 Or StartPosition > Len(String$) + 1
StartPosition = Len(String$)
EndIf
If ModeCase < 0
ModeCase = 0
ElseIf ModeCase > 1
ModeCase = 1
EndIf
If ModeCase = 0
String$ = Lower(String$)
StringToFind$ = Lower(StringToFind$)
EndIf
For Index = StartPosition To Len(String$) Step 1
Word$ = Mid(String$, Index, Len(StringToFind$))
If Word$ = StringToFind$
FindWord = Index
Exit
EndIf
Next
EndFunction FindWord
Function ReverseString(String$, Separator$, Option)
If Len(Separator$) = 1
SeparatorNumber = 0
StringReverse$ = ""
word$ = ""
Char$ = ""
If Option < 0
Option = 0
ElseIf Option > 1
Option = 1
EndIf
Select Option
Case 0
For Index = Len(String$) To 1 Step -1
StringReverse$ = StringReverse$ + Mid(String$, Index, 1)
Next
EndCase
Case 1
SeparatorNumber = CountString(String$, Separator$, 0)
If SeparatorNumber > 0
For Index = Len(String$) To 1 Step -1
Char$ = Mid(String$, Index, 1)
If Char$ <> Separator$ And Index > 1
Word$ = Word$ + Char$
ElseIf Char$ = Separator$ Or Index = 1
If Index = 1 And Char$ <> Separator$
Word$ = Word$ + Char$
EndIf
If Word$ > ""
For Index2 = Len(Word$) To 1 Step -1
StringReverse$ = StringReverse$ + Mid(Word$, Index2, 1)
Next
If Char$ = Separator$
StringReverse$ = StringReverse$ + Separator$
Else
StringReverse$ = StringReverse$
EndIf
Word$ = ""
ElseIf Word$ = ""
If Char$ = Separator$
StringReverse$ = StringReverse$ + Separator$
EndIf
EndIf
EndIf
Next
Else
For Index = Len(String$) To 1 Step -1
StringReverse$ = StringReverse$ + Mid(String$, Index, 1)
Next
EndIf
EndCase
EndSelect
Else
StringReverse$ = String$
EndIf
EndFunction StringReverse$
Function InsertString(String$, StringToInsert$, Position)
If Position = 0
Position = 1
ElseIf Position = -1 Or Position > Len(String$)
Position = Len(String$) + 1
ElseIf Position < -1
Position = 1
EndIf
String$ = Mid(String$, 1, Position - 1) + StringToInsert$ + Mid(String$, Position, -1)
EndFunction String$
Function RemoveString(String$, RemoveString$, StartPosition, NumberOccurences, ModeCase)
NumberRemoveOccurences = 0
Word$ = ""
Remove = 0
If StartPosition = 0
StartPosition = 1
ElseIf StartPosition = -1 Or StartPosition > Len(String$) + 1
StartPosition = Len(String$)
EndIf
If NumberOccurences <= 0
NumberOccurences = 1
EndIf
If ModeCase < 0
ModeCase = 0
ElseIf ModeCase > 1
ModeCase = 1
EndIf
If StartPosition <= Len(String$) And Len(RemoveString$) <= Len(String$)
For Index = StartPosition To Len(String$) Step 1
Word$ = Mid(String$, Index, Len(RemoveString$))
Remove = 0
If ModeCase = 0
If Lower(Word$) = Lower(RemoveString$)
Remove = 1
EndIf
ElseIf ModeCase = 1
If Word$ = RemoveString$
Remove = 1
EndIf
EndIf
If Remove = 1
If NumberRemoveOccurences + 1 <= NumberOccurences
String$ = Mid(String$, 1, Index - 1) + Mid(String$, Index + Len(Word$), -1)
Index = Index - Len(Word$)
NumberRemoveOccurences = NumberRemoveOccurences + 1
Else
Exit
EndIf
EndIf
Next
EndIf
EndFunction String$
Function ReplaceString(String$, StringToFind$, StringToReplace$, StartPosition, NumberOccurences, ModeCase)
NumberRemoveOccurences = 0
Word$ = ""
Replace = 0
If StartPosition = 0
StartPosition = 1
ElseIf StartPosition = -1 Or StartPosition > Len(String$) + 1
StartPosition = Len(String$)
EndIf
If NumberOccurences <= 0
NumberOccurences = 1
EndIf
If ModeCase < 0
ModeCase = 0
ElseIf ModeCase > 1
ModeCase = 1
EndIf
If StartPosition <= Len(String$) And Len(StringToFind$) <= Len(String$) And Len(StringToReplace$) <= Len(String$)
For Index = StartPosition To Len(String$) Step 1
Word$ = Mid(String$, Index, Len(StringToFind$))
Replace = 0
If ModeCase = 0
If Lower(Word$) = Lower(StringToFind$)
Replace = 1
EndIf
ElseIf ModeCase = 1
If Word$ = StringToFind$
Replace = 1
EndIf
EndIf
If Replace = 1
If NumberRemoveOccurences + 1 <= NumberOccurences
String$ = Mid(String$, 1, Index - 1) + StringToReplace$ + Mid(String$, Index + Len(Word$), -1)
Index = Index + Len(Word$)
NumberRemoveOccurences = NumberRemoveOccurences + 1
Else
Exit
EndIf
EndIf
Next
EndIf
EndFunction String$
Function CopyString(String$, StartPosition, EndPosition, PositionToCopy, NumberOfCopy)
StringToCopy$ = ""
If StartPosition = 0
StartPosition = 1
ElseIf StartPosition = -1 Or StartPosition > Len(String$) + 1
StartPosition = Len(String$)
EndIf
If EndPosition = 0
EndPosition = 1
ElseIf EndPosition = -1 Or EndPosition > Len(String$) + 1
EndPosition = Len(String$) + 1
ElseIf EndPosition < StartPosition
EndPosition = StartPosition
EndIf
If PositionToCopy = 0
PositionToCopy = 1
ElseIf PositionToCopy = -1 Or PositionToCopy > Len(String$) + 1
PositionToCopy = Len(String$) + 1
ElseIf PositionToCopy < -1
PositionToCopy = 1
EndIf
If NumberOfCopy <= 0
NumberOfCopy = 1
Endif
StringToCopy$ = Mid(String$, StartPosition, EndPosition)
For Copy = 1 To NumberOfCopy
String$ = Mid(String$, 1, PositionToCopy -1) + StringToCopy$ + Mid(String$, PositionToCopy, -1)
Next
EndFunction String$
Function CutString(String$, StartPosition, EndPosition)
If StartPosition = 0
StartPosition = 1
ElseIf StartPosition = -1 Or StartPosition > Len(String$) + 1
StartPosition = Len(String$)
EndIf
If EndPosition = 0
EndPosition = 1
ElseIf EndPosition = -1 Or EndPosition > Len(String$) + 1
EndPosition = Len(String$) + 1
ElseIf EndPosition < StartPosition
EndPosition = StartPosition
EndIf
String$ = Mid(String$, 1, StartPosition - 1) + Mid(String$, EndPosition + 1, -1)
EndFunction String$
Exemples:
// set window properties
SetWindowTitle( "Exemples..." )
SetWindowSize( 1400, 768, 0 )
// set display properties
SetVirtualResolution( 1400, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetClearColor(255, 255, 255)
SetPrintSize(16)
SetPrintColor(0, 0, 0)
Do
String1$ = "This is a string"
String2$ = "a string"
String3$ = "This a string"
String4$ = "This is a"
String5$ = ""
Print("Number of " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + "No Case = " + Str(CountString(String1$, "i", 0)))
PrintC("")
Print("Number of " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + "With Case On = " + Str(CountString(String1$, "I", 1)))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Find String No Case " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " = " + Str(FindString(String1$, "i", 3, 0)))
PrintC("")
Print("Find String No Case " + Chr(34) + "I" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " = " + Str(FindString(String1$, "I", 3, 0)))
PrintC("")
Print("Find String With Case On " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " = " + Str(FindString(String1$, "i", 3, 1)))
PrintC("")
Print("Find String With Case On " + Chr(34) + "I" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " = " + Str(FindString(String1$, "I", 3, 1)))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Reverse String With Option 0 " + Chr(34) + String1$ + Chr(34) + " = " + ReverseString(String1$, " ", 0))
PrintC("")
Print("Reverse String With Option 1 " + Chr(34) + String1$ + Chr(34) + " = " + ReverseString(String1$, " ", 1))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Insert String " + Chr(34) + "This is " + Chr(34) + " Into " + Chr(34) + String2$ + Chr(34) + " At Position 1 = " + InsertString(String2$, "This is ", 1))
PrintC("")
Print("Insert String " + Chr(34) + "is " + Chr(34) + " Into " + Chr(34) + String3$ + Chr(34) + " At Position 6 = " + InsertString(String3$, "is ", 6))
PrintC("")
Print("Insert String " + Chr(34) + " string" + Chr(34) + " Into " + Chr(34) + String4$ + Chr(34) + " At Position -1 = " + InsertString(String4$, " string", -1))
PrintC("")
Print("Insert String " + Chr(34) + "This is a string" + Chr(34) + " Into " + Chr(34) + String5$ + Chr(34) + " At Position 0 = " + InsertString(String5$, "This is a string", 0))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Remove String " + Chr(34) + "this" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, No Case, 1 Times = " + RemoveString(String1$, "this", 1, 1, 0))
PrintC("")
Print("Remove String " + Chr(34) + "this" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, With Case On, 1 Times = " + RemoveString(String1$, "this", 1, 1, 1))
PrintC("")
Print("Remove String " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, No Case, 2 Times = " + RemoveString(String1$, "i", 1, 2, 0))
PrintC("")
Print("Remove String " + Chr(34) + "I" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, No Case, 2 Times = " + RemoveString(String1$, "I", 1, 2, 0))
PrintC("")
Print("Remove String " + Chr(34) + "i" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, With Case On, 2 Times = " + RemoveString(String1$, "i", 1, 2, 1))
PrintC("")
Print("Remove String " + Chr(34) + "I" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, With Case On, 2 Times = " + RemoveString(String1$, "I", 1, 2, 1))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Replace String " + Chr(34) + "this" + Chr(34) + " By " + Chr(34) + "ThIs" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, No Case, 1 Times = " + ReplaceString(String1$, "this", "ThIs", 1, 1, 0))
PrintC("")
Print("Replace String " + Chr(34) + "ThIs" + Chr(34) + " By " + Chr(34) + "THIS" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, No Case, 1 Times = " + ReplaceString(String1$, "ThIs", "THIS", 1, 1, 0))
PrintC("")
Print("Replace String " + Chr(34) + "this" + Chr(34) + " By " + Chr(34) + "ThIs" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, With Case On, 1 Times = " + ReplaceString(String1$, "this", "ThIs", 1, 1, 1))
PrintC("")
Print("Replace String " + Chr(34) + "This" + Chr(34) + " By " + Chr(34) + "THIS" + Chr(34) + " Into " + Chr(34) + String1$ + Chr(34) + " At Position 1, With Case On, 1 Times = " + ReplaceString(String1$, "This", "THIS", 1, 1, 1))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Copy String " + Chr(34) + "This" + Chr(34) + " From The Position 1 To 5 Into " + Chr(34) + String1$ + Chr(34) + " At Position 6, 3 Times = " + CopyString(String1$, 1, 5, 6, 3))
PrintC("")
Print("Copy String " + Chr(34) + "This" + Chr(34) + " From The Position 1 To 5 Into " + Chr(34) + String1$ + Chr(34) + " At Position -1, 3 Times = " + CopyString(String1$, 1, 5, -1, 3))
PrintC("")
Print("---------------------------------------------------")
PrintC("")
Print("Cut String " + Chr(34) + "T" + Chr(34) + " From The Position 1 To 1 Into " + Chr(34) + String1$ + Chr(34) + " = " + CutString(String1$, 1, 1))
PrintC("")
Print("Cut String " + Chr(34) + "This " + Chr(34) + " From The Position 1 To 5 Into " + Chr(34) + String1$ + Chr(34) + " = " + CutString(String1$, 1, 5))
PrintC("")
Print("Cut String " + Chr(34) + "g" + Chr(34) + " From The Position -1 To -1 Into " + Chr(34) + String1$ + Chr(34) + " = " + CutString(String1$, -1, -1))
Sync()
Loop
I am French and I know nothing about English.
So if you grieve not understand them very well.