I couldn't find any tips for this here, so here's mine... it emulates Visual
Basic's vbProperCase flag to create strings with capital letters on each
word. (I just needed it to rename some MP3 files to a neat format).
Procedure.s ProperCase(text$)
For r=1 To Len(text$)
pre=Asc(LCase(Mid(text$,r-1,1)))
cur=Asc(LCase(Mid(text$,r,1)))
If cur>96 And cur<123 And (r=1 Or pre<97 Or pre>122)
cur-32
EndIf
a$+Chr(cur)
Next
ProcedureReturn a$
EndProcedure
Debug ProperCase("ThIs IS a tEst")
illegal?
The very first pass through the loop, "pre" is set to the value of the "0th" character of the string.
I thought that positions for string functions start at 1, not 0?
I notice that the PB compiler accepts this syntax, though.
It seems that using 0 or 1 in Mid() will give you the first character of the string.
Procedure.s ProperCase(text$)
a$ = ucase(Mid(text$,1,1))
For r=2 To Len(text$)
cur=Asc(LCase(Mid(text$,r,1)))
pre=Asc(LCase(Mid(text$,r-1,1)))
If cur>96 And cur<123 And (r=1 Or pre<97 Or pre>122)
cur-32
EndIf
a$+Chr(cur)
Next
ProcedureReturn a$
EndProcedure
Procedure.s MCase(somestr.s)
temp.s=" "+Lower(somestr)
For a=Asc("a") To Asc("z")
olds.s=" "+Chr(a)
news.s=" "+Chr(a+32)
ReplaceString(temps,olds,news)
next
temps=Mid(temps,2,Len(somestr))
ProcedureReturn temps
EndProcedure
In this way, the loop only repeats 26 times, once for each letter,
regardless of the length of somestr. Putting a space at the beginning of Temps ensures that the first letter of the string is treated properly.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Procedure.s MyCase(text.s)
*ptr.Byte = @text
pre.l = 32
While *ptr\b <> 0
If pre = 32
pre = *ptr\b & $ff
If pre >= 'a' And pre <= 'z'
pre-32
*ptr\b = pre
EndIf
Else
pre = *ptr\b & $ff
If pre >= 'A' And pre <= 'Z'
pre+32
*ptr\b = pre
EndIf
EndIf
*ptr+1
Wend
ProcedureReturn text
EndProcedure
[Edit] Changed code a bit...[/Edit]
Last edited by Pupil on Mon Apr 12, 2004 2:21 pm, edited 1 time in total.
Procedure.s x_propercase(s.s)
l = Len(s)
*p = @s
n = 0
f = 1
While n < l
b = PeekB(*p+n)
If b = 32
f = 1
ElseIf f = 1 And b >= 97 And b<=122
PokeB(*p+n,b & $DF)
f = 0
ElseIf f = 0 And b >= 65 And b <= 90
PokeB(*p+n,b | $20)
f = 0
Else
f = 0
EndIf
n = n+1
Wend
ProcedureReturn s
EndProcedure
Debug x_propercase("ThIs IS a tEst")
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Procedure.s x_propercase(s.s)
*p = @s
f = 1
b = PeekB(*p)
While b <> 0
If b = 32
f = 1
ElseIf f = 1 And b >= 97 And b<=122
PokeB(*p,b & $DF)
f = 0
ElseIf f = 0 And b >= 65 And b <= 90
PokeB(*p,b | $20)
f = 0
Else
f = 0
EndIf
*p = *p+1
b = PeekB(*p)
Wend
ProcedureReturn s
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
(wrote a fairly good centipee clone called multibug for the vic20... without an assembler! if anybody has a copy of that game i would feel *very* obliged, lost my own copy though i have a vic20 standing here since a few months)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )