How do I get the leftmost/righmost character in a string?

Just starting out? Need help? Post your questions and find answers here.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How do I get the leftmost/righmost character in a string?

Post by #NULL »

@mk-soft
Your ProcedureReturn values should be swapped?
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How do I get the leftmost/righmost character in a string?

Post by mk-soft »

@idle
I have already prepared something for your modules

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)

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How do I get the leftmost/righmost character in a string?

Post by mk-soft »

#NULL wrote: Sat May 06, 2023 11:30 am @mk-soft
Your ProcedureReturn values should be swapped?
I don't think so, as it is no longer a UCS-2 string
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How do I get the leftmost/righmost character in a string?

Post by idle »

mk-soft wrote: Sat May 06, 2023 11:41 am @idle
I have already prepared something for your modules

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)

Thanks looks good 👍 I'll add it tomorrow
The8th
User
User
Posts: 29
Joined: Fri Sep 04, 2015 10:23 am

Re: How do I get the leftmost/righmost character in a string?

Post by The8th »

Thank you guys that you invest so much efforts in my problem.
But I do not want to include extra modules and procedures in my code. My goal is to simply test if there is one of these strange characters on the right end of my strings. My strings involved only contain one of these characters each, and I can use

Code: Select all

If Right(mystring$, 2) = "🅐"
this works in this case.
Also

Code: Select all

If FindString(mystring$, "🅐")
works. And even

Code: Select all

RemoveString(mystring$, "🅚")
ReplaceString(mystring$, "🅚", "nothing")
work.
Nevertheless it's strange and a pity that it does not work with all PB string functions.
Henry
Olli
Addict
Addict
Posts: 1199
Joined: Wed May 27, 2020 12:26 pm

Re: How do I get the leftmost/righmost character in a string?

Post by Olli »

UTF is too complex ! I stopped my read to Unicode 0xD83C. Now, I suppose the IDE does not support it...
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How do I get the leftmost/righmost character in a string?

Post by STARGÅTE »

I already posted some supplementary functions in the german board:
Characters above the Basic Multilingual Plane (BMP) plane
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Olli
Addict
Addict
Posts: 1199
Joined: Wed May 27, 2020 12:26 pm

Re: How do I get the leftmost/righmost character in a string?

Post by Olli »

Thank you ! (Die Zeit verbringt... :cry: )
Post Reply