RIGHT$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The '''RIGHT$''' function returns a set number of characters in a STRING variable starting from the end and counting backwards. {{PageSyntax}} :: '''RIGHT$('''''stringvalue$, numberofcharacters%''''')''' {{Parameters}} * The ''stringvalue$'' can be any string of ASCII characters as a STRING variable. * The ''numberofcharacters'' INTEGER value determines the number of characters to return from the right end of the string. {{PageDescription}} * If th...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''RIGHT$''' function returns a set number of characters in a [[STRING]] variable starting from the end and counting backwards.
The '''RIGHT$''' function returns a set number of characters in a [[STRING]] variable starting from the end and counting backwards.




Line 7: Line 6:




{{Parameters}}
{{PageParameters}}
* The ''stringvalue$'' can be any string of [[ASCII]] characters as a [[STRING]] variable.
* The ''stringvalue$'' can be any string of [[ASCII]] characters as a [[STRING]] variable.
* The ''numberofcharacters'' [[INTEGER]] value determines the number of characters to return from the right end of the string.
* The ''numberofcharacters'' [[INTEGER]] value determines the number of characters to return from the right end of the string.
Line 18: Line 17:




''Example 1:'' Getting the right portion of a string value such as a person's last name.  
''Example 1:'' Getting the right portion of a string value such as a person's last name.
{{CodeStart}} '' ''
{{CodeStart}}
name$ = "Tom Williams"
name$ = "Tom Williams"


Last$ = {{Cl|RIGHT$}}(name$, {{Cl|LEN}}(name$) - {{Cl|INSTR}}(name$, " ")) 'subtract space position from string length
Last$ = {{Cl|RIGHT$}}(name$, {{Cl|LEN}}(name$) - {{Cl|INSTR}}(name$, " ")) 'subtract space position from string length


{{Cl|PRINT}} Last$ '' ''
{{Cl|PRINT}} Last$
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Williams {{OutputEnd}}
{{OutputStart}}Williams {{OutputEnd}}
Line 30: Line 29:


''Example 2:'' Adding the leading zero in single digit [[HEX$]] values using RIGHT to take the right two hexadecimal string digits.
''Example 2:'' Adding the leading zero in single digit [[HEX$]] values using RIGHT to take the right two hexadecimal string digits.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 32) '32 bit screen modes ONLY!
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 32) '32 bit screen modes ONLY!
red = 255
red = 255
Line 44: Line 43:
{{Cl|PRINT}} hexadecimal$
{{Cl|PRINT}} hexadecimal$
{{Cl|COLOR}} {{Cl|VAL}}(hexadecimal$)
{{Cl|COLOR}} {{Cl|VAL}}(hexadecimal$)
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}'''{{text|&HFFFF0080|white}}'''
{{OutputStart}}'''{{Text|&HFFFF0080|white}}'''
'''{{text|Colored text|#FF0080}}'''{{OutputEnd}}
'''{{Text|Colored text|#FF0080}}'''{{OutputEnd}}
: ''Note:'' When a single hexadecimal digit is returned the resulting value will need the leading zero added. Otherwise the hexa- decimal value created will have a byte missing from the value. EX: Color &HFF000000 is valid while &HFF000 is not.
: ''Note:'' When a single hexadecimal digit is returned the resulting value will need the leading zero added. Otherwise the hexa- decimal value created will have a byte missing from the value. EX: Color &HFF000000 is valid while &HFF000 is not.




''See also:''
{{PageSeeAlso}}
* [[LEFT$]], [[MID$]]  
* [[LEFT$]], [[MID$ (function)]]
* [[LTRIM$]], [[RTRIM$]]  
* [[LTRIM$]], [[RTRIM$]]
* [[INSTR]], [[HEX$]]
* [[INSTR]], [[HEX$]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:43, 26 February 2023

The RIGHT$ function returns a set number of characters in a STRING variable starting from the end and counting backwards.


Syntax

RIGHT$(stringvalue$, numberofcharacters%)


Parameters

  • The stringvalue$ can be any string of ASCII characters as a STRING variable.
  • The numberofcharacters INTEGER value determines the number of characters to return from the right end of the string.


Description

  • If the number of characters exceeds the string length(LEN) the entire string is returned.
  • RIGHT$ returns always start at the last character of the string, even if a space. RTRIM$ can remove ending spaces.
  • Number of characters cannot be a negative value.


Example 1: Getting the right portion of a string value such as a person's last name.

name$ = "Tom Williams"

Last$ = RIGHT$(name$, LEN(name$) - INSTR(name$, " ")) 'subtract space position from string length

PRINT Last$
Williams 


Example 2: Adding the leading zero in single digit HEX$ values using RIGHT to take the right two hexadecimal string digits.

SCREEN _NEWIMAGE(640, 480, 32) '32 bit screen modes ONLY!
red = 255
green = 0
blue = 128

Color32 red, green, blue
PRINT "Colored text"

SUB Color32 (R, G, B)
R = R AND &HFF: G = G AND &HFF: B = B AND &HFF '    limit values to 0 to 255
hexadecimal$ = "&HFF" + RIGHT$("0" + HEX$(R), 2) + RIGHT$("0" + HEX$(G), 2) + RIGHT$("0" + HEX$(B), 2)
PRINT hexadecimal$
COLOR VAL(hexadecimal$)
END SUB
&HFFFF0080
Colored text
Note: When a single hexadecimal digit is returned the resulting value will need the leading zero added. Otherwise the hexa- decimal value created will have a byte missing from the value. EX: Color &HFF000000 is valid while &HFF000 is not.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link