Re: How do I get the leftmost/righmost character in a string?
Posted: Sat May 06, 2023 11:30 am
@mk-soft
Your ProcedureReturn values should be swapped?
Your ProcedureReturn values should be swapped?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
;-TOP by mk-soft
Procedure IsUTF16String(String$)
Protected *String.Unicode = @String$
If *String
While *String\u
If *String\u > $D7FF And *String\u < $E000
ProcedureReturn #True
EndIf
*String + 2
Wend
EndIf
ProcedureReturn #False
EndProcedure
Procedure LenUTF16(String$)
Protected *Char.Unicode
Protected cnt
*Char.Unicode = @String$
If *Char
While *Char\u
If *Char\u > $D7FF And *Char\u < $E000
*Char + 4
Else
*Char + 2
EndIf
cnt + 1
Wend
EndIf
ProcedureReturn cnt
EndProcedure
Procedure.s LeftUTF16(String$, Length)
Protected *Char.Unicode
Protected len, cnt
If Length < 1
ProcedureReturn ""
EndIf
*Char.Unicode = @String$
If *Char
While *Char\u
If cnt >= Length
Break
EndIf
If *Char\u > $D7FF And *Char\u < $E000
*Char + 4
len + 2
Else
*Char + 2
len + 1
EndIf
cnt + 1
Wend
EndIf
ProcedureReturn Left(String$, len)
EndProcedure
Procedure.s RightUTF16(String$, Length)
Protected *Char.Unicode, *Char2.Unicode, *String.Unicode
Protected len, cnt
If Length < 1
ProcedureReturn ""
EndIf
*String = @String$
If *String
*Char = *String + StringByteLength(String$) - 2
While *Char\u
If cnt >= Length Or *Char <= *String
Break
EndIf
*Char2 = *Char - 2
If *Char2 >= *String And (*Char\u > $D7FF And *Char\u < $E000)
*Char - 4
len + 2
Else
*Char - 2
len + 1
EndIf
cnt + 1
Wend
EndIf
ProcedureReturn Right(String$, len)
EndProcedure
; ****
Define s1.s
s1 = "π
Aπ
Kπ
"
Debug "Is '" + s1 + "' UFT16: " + IsUTF16String(s1)
Debug "StringByteLength = " + StringByteLength(s1)
Debug "Len = " + LenUTF16(s1)
Debug "Left = " + LeftUTF16(s1, 2)
Debug "Right = " + RightUTF16(s1, 2)
I don't think so, as it is no longer a UCS-2 string
Thanks looks goodmk-soft wrote: Sat May 06, 2023 11:41 am @idle
I have already prepared something for your modulesCode: Select all
;-TOP by mk-soft Procedure IsUTF16String(String$) Protected *String.Unicode = @String$ If *String While *String\u If *String\u > $D7FF And *String\u < $E000 ProcedureReturn #True EndIf *String + 2 Wend EndIf ProcedureReturn #False EndProcedure Procedure LenUTF16(String$) Protected *Char.Unicode Protected cnt *Char.Unicode = @String$ If *Char While *Char\u If *Char\u > $D7FF And *Char\u < $E000 *Char + 4 Else *Char + 2 EndIf cnt + 1 Wend EndIf ProcedureReturn cnt EndProcedure Procedure.s LeftUTF16(String$, Length) Protected *Char.Unicode Protected len, cnt If Length < 1 ProcedureReturn "" EndIf *Char.Unicode = @String$ If *Char While *Char\u If cnt >= Length Break EndIf If *Char\u > $D7FF And *Char\u < $E000 *Char + 4 len + 2 Else *Char + 2 len + 1 EndIf cnt + 1 Wend EndIf ProcedureReturn Left(String$, len) EndProcedure Procedure.s RightUTF16(String$, Length) Protected *Char.Unicode, *Char2.Unicode, *String.Unicode Protected len, cnt If Length < 1 ProcedureReturn "" EndIf *String = @String$ If *String *Char = *String + StringByteLength(String$) - 2 While *Char\u If cnt >= Length Or *Char <= *String Break EndIf *Char2 = *Char - 2 If *Char2 >= *String And (*Char\u > $D7FF And *Char\u < $E000) *Char - 4 len + 2 Else *Char - 2 len + 1 EndIf cnt + 1 Wend EndIf ProcedureReturn Right(String$, len) EndProcedure ; **** Define s1.s s1 = "π Aπ Kπ " Debug "Is '" + s1 + "' UFT16: " + IsUTF16String(s1) Debug "StringByteLength = " + StringByteLength(s1) Debug "Len = " + LenUTF16(s1) Debug "Left = " + LeftUTF16(s1, 2) Debug "Right = " + RightUTF16(s1, 2)
Code: Select all
If Right(mystring$, 2) = "π
"
Code: Select all
If FindString(mystring$, "π
")
Code: Select all
RemoveString(mystring$, "π
")
ReplaceString(mystring$, "π
", "nothing")