Capitalize a String

Share your advanced PureBasic knowledge/code with the community.
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Capitalize a String

Post by SniffTheGlove »

Could not find a built in procedure to capitalize a string in the help file or here on the forum, so I put one together and posted it here for other to use as well if needed

Code: Select all

Procedure.s CapitalizeString(String2Convert.s)
  CountofSpaces.l =  CountString(String2Convert.s," ")
  ReBuildString.s = ""
For i.l = 1  To CountofSpaces.l + 1
  tmpString.s = StringField(String2Convert.s, i.l, " ")
  tmpCapitalize.s = UCase(Left(tmpString.s, 1))
  tmpLowerCase.s = LCase(Right(tmpString.s, Len(tmpString) - 1))
  ReBuildString.s = ReBuildString.s + tmpCapitalize + tmpLowerCase.s + " "
Next
ProcedureReturn Trim(ReBuildString.s)
EndProcedure
  
Debug CapitalizeString("we Went to neW yorK today to buy sausages")
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Capitalize a String

Post by luis »

Thank you.

This is a little lighter tough, does not use left(),right(),len(),countstring(),stringfield() ...

Code: Select all

 Procedure.s CapitalizeString_2 (str$)
 Protected *p.Character = @str$
 Protected iNewWord = 1
 Protected t$, out$
  
 While *p\c
    t$ = PeekS(*p,1)
    If iNewWord
        out$ + UCase(t$)
        iNewWord = 0
    Else
        out$ + LCase(t$)
    EndIf
    If t$ = " "
        iNewWord = 1
    EndIf    
    *p + SizeOf(Character)
 Wend
 
 ProcedureReturn Trim(out$)
EndProcedure 

Define str$ = "  w  we   we Went to neW yorK toDay to buy       sausages  "

Debug CapitalizeString_2(str$)

It seem to be compatible with yours.

PS: a suggestion, drop the habit to use .l unless really needed :wink:
http://www.purebasic.fr/blog/?p=42

PS2: another remark, sooner or later not using EnableExplicit will come back to haunt you :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Capitalize a String

Post by MachineCode »

SniffTheGlove wrote:Could not find a built in procedure to capitalize a string in the help file or here on the forum
A search for "capitalize string" shows these two previous threads: http://www.purebasic.fr/english/viewtop ... 12&t=24496 and http://www.purebasic.fr/english/viewtop ... 12&t=10196 . Trust me: almost everything has been discussed before. :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Re: Capitalize a String

Post by SniffTheGlove »

MachineCode wrote:
SniffTheGlove wrote:Could not find a built in procedure to capitalize a string in the help file or here on the forum
A search for "capitalize string" shows these two previous threads: http://www.purebasic.fr/english/viewtop ... 12&t=24496 and http://www.purebasic.fr/english/viewtop ... 12&t=10196 . Trust me: almost everything has been discussed before. :)
You are right, it appears that I could not spell correctly when I was searching as I just checked the search box history to see I had misspelled Capitalize as Capitialize. No wonder I could not see anything. :D
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Re: Capitalize a String

Post by SniffTheGlove »

luis wrote:Thank you.

This is a little lighter tough, does not use left(),right(),len(),countstring(),stringfield() ...

Code: Select all

 Procedure.s CapitalizeString_2 (str$)
 Protected *p.Character = @str$
 Protected iNewWord = 1
 Protected t$, out$
  
 While *p\c
    t$ = PeekS(*p,1)
    If iNewWord
        out$ + UCase(t$)
        iNewWord = 0
    Else
        out$ + LCase(t$)
    EndIf
    If t$ = " "
        iNewWord = 1
    EndIf    
    *p + SizeOf(Character)
 Wend
 
 ProcedureReturn Trim(out$)
EndProcedure 

Define str$ = "  w  we   we Went to neW yorK toDay to buy       sausages  "

Debug CapitalizeString_2(str$)

It seem to be compatible with yours.

PS: a suggestion, drop the habit to use .l unless really needed :wink:
http://www.purebasic.fr/blog/?p=42

PS2: another remark, sooner or later not using EnableExplicit will come back to haunt you :wink:
Thanks, I will take notice of the removal of .l from the variable names, i am new to PB and was just following convention on some previous code someone else had written quite a while back.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Capitalize a String

Post by STARGÅTE »

here very fast code without strings in procedure (ascii only):

Code: Select all

Structure CharacterArray
	c.c[0]
EndStructure

Procedure FastCapitalizeString(*String.Character)
	*Up.CharacterArray = ?UpCharacters
	*Low.CharacterArray = ?LowCharacters
	If *String\c
		*String\c = *Up\c[*String\c]
		Repeat
			*String + SizeOf(Character)
			If *String\c = ' '
				Repeat
					*String + SizeOf(Character)
				Until *String\c <> ' '
				*String\c = *Up\c[*String\c]
			Else
				*String\c = *Low\c[*String\c]
			EndIf
		Until *String\c = #NUL
	EndIf
EndProcedure 

Define String.s = "w  we   we Went to neW yorK toDay to buy       sausages  "

FastCapitalizeString(@String)

Debug String

DataSection
	UpCharacters:
	Data.c $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F
	Data.c $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F
	Data.c $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
	Data.c $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
	Data.c $40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$4A,$4B,$4C,$4D,$4E,$4F
	Data.c $50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5A,$5B,$5C,$5D,$5E,$5F
	Data.c $60,$41,$42,$43,$44,$45,$46,$47,$48,$49,$4A,$4B,$4C,$4D,$4E,$4F
	Data.c $50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$5A,$7B,$7C,$7D,$7E,$7F
	Data.c $80,$81,$82,$83,$84,$85,$86,$87,$88,$89,$8A,$8B,$8C,$8D,$8E,$8F
	Data.c $90,$91,$92,$93,$94,$95,$96,$97,$98,$99,$8A,$9B,$8C,$9D,$8E,$9F
	Data.c $A0,$A1,$A2,$A3,$A4,$A5,$A6,$A7,$A8,$A9,$AA,$AB,$AC,$AD,$AE,$AF
	Data.c $B0,$B1,$B2,$B3,$B4,$B5,$B6,$B7,$B8,$B9,$BA,$BB,$BC,$BD,$BE,$BF
	Data.c $C0,$C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$C9,$CA,$CB,$CC,$CD,$CE,$CF
	Data.c $D0,$D1,$D2,$D3,$D4,$D5,$D6,$D7,$D8,$D9,$DA,$DB,$DC,$DD,$DE,$DF
	Data.c $C0,$C1,$C2,$C3,$C4,$C5,$C6,$C7,$C8,$C9,$CA,$CB,$CC,$CD,$CE,$CF
	Data.c $D0,$D1,$D2,$D3,$D4,$D5,$D6,$F7,$D8,$D9,$DA,$DB,$DC,$DD,$DE,$9F
	LowCharacters:
	Data.c $00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,$0B,$0C,$0D,$0E,$0F
	Data.c $10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F
	Data.c $20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,$2C,$2D,$2E,$2F
	Data.c $30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F
	Data.c $40,$61,$62,$63,$64,$65,$66,$67,$68,$69,$6A,$6B,$6C,$6D,$6E,$6F
	Data.c $70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$7A,$5B,$5C,$5D,$5E,$5F
	Data.c $60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$6A,$6B,$6C,$6D,$6E,$6F
	Data.c $70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$7A,$7B,$7C,$7D,$7E,$7F
	Data.c $80,$81,$82,$83,$84,$85,$86,$87,$88,$89,$9A,$8B,$9C,$8D,$9E,$8F
	Data.c $90,$91,$92,$93,$94,$95,$96,$97,$98,$99,$9A,$9B,$9C,$9D,$9E,$FF
	Data.c $A0,$A1,$A2,$A3,$A4,$A5,$A6,$A7,$A8,$A9,$AA,$AB,$AC,$AD,$AE,$AF
	Data.c $B0,$B1,$B2,$B3,$B4,$B5,$B6,$B7,$B8,$B9,$BA,$BB,$BC,$BD,$BE,$BF
	Data.c $E0,$E1,$E2,$E3,$E4,$E5,$E6,$E7,$E8,$E9,$EA,$EB,$EC,$ED,$EE,$EF
	Data.c $F0,$F1,$F2,$F3,$F4,$F5,$F6,$D7,$F8,$F9,$FA,$FB,$FC,$FD,$FE,$DF
	Data.c $E0,$E1,$E2,$E3,$E4,$E5,$E6,$E7,$E8,$E9,$EA,$EB,$EC,$ED,$EE,$EF
	Data.c $F0,$F1,$F2,$F3,$F4,$F5,$F6,$F7,$F8,$F9,$FA,$FB,$FC,$FD,$FE,$FF
EndDataSection
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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Capitalize a String

Post by wilbert »

ASM variation on the code above

Code: Select all

#CharSize = SizeOf(Character)

Procedure FastCapitalizeString(*String)
  EnableASM
  !stc
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !lea rcx, [.CapChars]
    MOV rdx, *String
    !.CapStrLoop:
    !movzx rax, byte [rdx]
    !rcl ah, 1
    !and al, al
    !jz .CapStrExit
    !mov al, byte [rax + rcx]
    !mov [rdx], al
    ADD rdx, #CharSize
  CompilerElse
    !lea ecx, [.CapChars]
    MOV edx, *String
    !.CapStrLoop:
    !movzx eax, byte [edx]
    !rcl ah, 1
    !and al, al
    !jz .CapStrExit
    !mov al, byte [eax + ecx]
    !mov [edx], al
    ADD edx, #CharSize
  CompilerEndIf
  !cmp al, 0x21
  !jmp .CapStrLoop
  
  !.CapChars:  
  ; lowercase
  !db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  !db 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
  !db 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F
  !db 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F
  !db 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F
  !db 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x5B,0x5C,0x5D,0x5E,0x5F
  !db 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F
  !db 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F
  !db 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x9A,0x8B,0x9C,0x8D,0x9E,0x8F
  !db 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0xFF
  !db 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF
  !db 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF
  !db 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF
  !db 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xD7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xDF
  !db 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF
  !db 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
  ; uppercase
  !db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  !db 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
  !db 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F
  !db 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F
  !db 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F
  !db 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F
  !db 0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F
  !db 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x7B,0x7C,0x7D,0x7E,0x7F
  !db 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F
  !db 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x8E,0x9F
  !db 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF
  !db 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF
  !db 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF
  !db 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF
  !db 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF
  !db 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F
  
  !.CapStrExit:
  DisableASM
EndProcedure


Define String.s = "w  we   we Went to neW yorK toDay to buy       sausages  "

FastCapitalizeString(@String)

Debug String
For a procedure accepting and returning a string

Code: Select all

#CharSize = SizeOf(Character)

Procedure.s CapitalizeString(String.s)
  EnableASM
  !stc
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !lea rcx, [.CapChars]
    MOV rdx, String
    !.CapStrLoop:
    !movzx rax, byte [rdx]
    !rcl ah, 1
    !and al, al
    !jz .CapStrExit
    !mov al, byte [rax + rcx]
    !mov [rdx], al
    ADD rdx, #CharSize
  CompilerElse
    !lea ecx, [.CapChars]
    MOV edx, String
    !.CapStrLoop:
    !movzx eax, byte [edx]
    !rcl ah, 1
    !and al, al
    !jz .CapStrExit
    !mov al, byte [eax + ecx]
    !mov [edx], al
    ADD edx, #CharSize
  CompilerEndIf
  !cmp al, 0x21
  !jmp .CapStrLoop
  
  !.CapChars:  
  ; lowercase
  !db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  !db 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
  !db 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F
  !db 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F
  !db 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F
  !db 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x5B,0x5C,0x5D,0x5E,0x5F
  !db 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F
  !db 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F
  !db 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x9A,0x8B,0x9C,0x8D,0x9E,0x8F
  !db 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0xFF
  !db 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF
  !db 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF
  !db 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF
  !db 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xD7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xDF
  !db 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF
  !db 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
  ; uppercase
  !db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  !db 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
  !db 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F
  !db 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F
  !db 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F
  !db 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F
  !db 0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F
  !db 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x7B,0x7C,0x7D,0x7E,0x7F
  !db 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F
  !db 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x8E,0x9F
  !db 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF
  !db 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF
  !db 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF
  !db 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF
  !db 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF
  !db 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F
  
  !.CapStrExit:
  DisableASM
  ProcedureReturn String
EndProcedure

Debug CapitalizeString("w  we   we Went to neW yorK toDay to buy       sausages  ")
Last edited by wilbert on Sun May 20, 2018 6:00 am, edited 3 times in total.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Capitalize a String

Post by Little John »

Variation of Luis' code, which does without string concatenation:

Code: Select all

EnableExplicit

Procedure.s CapitalizeString_2a (str$)
   Protected *p.Character = @str$
   Protected newWord.i = #True
   
   While *p\c
      If newWord
         *p\c = Asc(UCase(Chr(*p\c)))
         newWord = #False
      Else
         *p\c = Asc(LCase(Chr(*p\c)))
      EndIf
      If *p\c = ' '
         newWord = #True
      EndIf   
      *p + SizeOf(Character)
   Wend
   
   ProcedureReturn Trim(str$)
EndProcedure


Define str$ = "  w  we   we Went to neW yorK toDay to buy       sausages  "

Debug CapitalizeString_2a(str$)
Regards, Little John
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Capitalize a String

Post by luis »

Hi Little John

Using Asc() didn't you make the effort to make it unicode compatible vain :?:

I did the way I did to make it work with unicode strings too, am I missing something ?
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Capitalize a String

Post by Little John »

Hi Luis,

in a PB program that is compiled in Unicode mode, Asc() returns the respective Unicode number ("code point"). Unfortunately, the name Asc() is rather misleading in this case.
in a PB program that is compiled in Unicode mode, Chr() returns the respective 2-byte-Unicode-character, if the source code is in UTF-8 format (which is my "private standard", so I forgot to mention it).
So I think ( hope :-) ) my modification of your code is fine.

Best regards, Little John
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Capitalize a String

Post by luis »

Little John wrote: in a PB program that is compiled in Unicode mode, Asc() returns the respective Unicode number ("code point"). Unfortunately, the name Asc() is rather misleading in this case.
Is that so ? I'm very glad to learn is working that way.
Did you read that somewhere or do you verified it by experimenting ?

The problem is not only in the name, the fact is I checked with the manual at the time and this is what it's saying:
help wrote: Return the Ascii value of the first character in the string.
... and so I avoided to use it with unicode strings.
It's not very open to interpretation (the return value should be in the range 0-127 or 0-255 at most), and if this is not the case in unicode mode the help should be corrected.

Thanks for the info :)
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Capitalize a String

Post by Little John »

Luis, you are most welcome.
luis wrote:The problem is not only in the name, the fact is I checked with the manual at the time and this is what it's saying:
help wrote: Return the Ascii value of the first character in the string.
Dooh, so the Help is still not updated.

I've read it in this thread (especially the last 3 posts).
In the year 2008, Fred wrote:I will update the doc.
Yes, please. :-)

Regards, Little John
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Capitalize a String

Post by Trond »

I made a recursive procedure.

Code: Select all

Procedure.s _Capitalize(S.s)
  If S
    If Asc(S) = ' '
      ProcedureReturn UCase(Left(S, 2)) + _Capitalize(Mid(S, 3))
    EndIf
    ProcedureReturn LCase(Chr(Asc(S))) + _Capitalize(Mid(S, 2))
  EndIf
EndProcedure

Procedure.s Capitalize(S.s)
  ProcedureReturn Trim(_Capitalize(" " + S))
EndProcedure


Define str$ = "  w  we   we Went to neW yorK toDay to buy       sausages  "

Debug Capitalize(str$)
Post Reply