====== MID$ ====== Returns a number of characters from a given starting position of a string /* For the lines below, just create links, separated by commas, to other pages of the BASICs section, like [[:basics:basicTagName]]. "Implemented by" should mention BASICs which implement the keyword like described, in a more usual and standard way. "With variations" should point to BASICs where this keyword is used a bit differently. "Also written as" should list alternative spellings and abbreviations this keyword has. No need to refer to a BASIC, there is a section for that later. */ **Implemented by:** [[:basics:Altair:altair8K]], [[:basics:Applesoft:]], [[:basics:CBM:]], [[:basics:msatari:]], [[:basics:trs80:level2]], [[:basics:trscolor:]], [[:basics:trscolor:colormc]], [[:basics:BBC:]], [[:basics:GWBASIC:]], [[:basics:bwbasic:]], [[:basics:bwbasic:bw32]], [[:basics:ABasiC:]], [[:basics:msqb:QBasic]], [[:basics:VBDOS:]], [[:basics:Gambas:]], [[:basics:minibasicmm:]], [[:basics:minibasicjs:]] **With variations:** [[:basics:Atari:atxl]] (position 0 causes error), [[:basics:trs80:trsdisk]] (can receive a string), [[:basics:trs80:level3]] (can rceive a string), [[:basics:MSX:]] (can be assigned a value, for substitution) **Also written as: ** ===== Usage ===== /* For inline examples of syntax, write the keyword between '' and '' (two single quotes) */ The basic syntax is: stringvar$ = MID$ (str, startpos, n) where ''str'' is the source string, ''startpos'' is the position of the first character of the desired substring (starting from left), and ''n'' is the number of characters to extract. /* Below is an example of a code block, using GeSHi syntax highlighting for //gwbasic//. Other styles of interest might be //basic4gl//, //blitzbasic//, //freebasic//, //gambas//, //locobasic//, //purebasic//, //qbasic//, //sdlbasic//, //thinbasic//, //vb//, //vbnet//, //xbasic// or //zxbasic//. 10 PRINT "HELLO" */ The following example is from the //[[:basics:msatari|Atari Microsoft BASIC]]// manual: 100 A$ = "GETTHEMIDDLE" 110 PRINT MID(A$,4,3) ==== Variations ==== As expected, the numerical arguments to MID$ may vary a lot: * ''startpos'' begins from 0 in [[:basics:msx]] * ''startpos'' begins from 1 in [[:basics:msatari:]] * Values for ''startpos'' and ''n'' must be in the 8-bit range of 0 to 255 for most 8-bit BASICs. Strings could not have more than 255 characters in most of them anyway. ''n'' is optional and, if ommited, all characters from ''startpos'' to the end of ''str'' are returned − this stands true for [[:basics:msx]]. If ''n'' from ''startpos'' surpasses the end of ''str'', most BASICs will simply return all characters from ''startpos'' to the end of ''str''. Watch out for differences in the effects of a negative value for ''n'' or ''startpos''. === MID$= for substitutions === Some BASICs, especially Microsoft-based ones such as [[:basics:msx|MSX BASIC]] and [[:basics:trs80:level3|TRS-80 Level III BASIC]], allow a strange syntax where MID$ //receives// a string value, which will //replace// the substring found by the function, effectively altering the source string ''str''. An example: 10 A$ = "ABC" 20 MID$(A$, 1, 1) = "Basic!" 30 PRINT A$ ===== Examples ===== ===== Comments ===== /* Origin of the keyword, opinions, history... */ In the Microsoft realm, the keyword first appeared in the 8k version of [[:basics:altair|Altair BASIC]]. ===== Related keywords ===== /* Keywords that are often or always used along this one */ * [[left-s|LEFT$]] and [[right-s|RIGHT$]] * [[INSTR]] as a way to find the first character of a substring ===== Similar keywords ===== /* Keywords with the same or similar functionality found in other versions of BASIC */ ===== In other languages... ===== /* For parallels and comparisons with other modern-day languages */ ===== References ===== /* If you used the ((citation)) syntax elsewhere in the text, they will appear under this section. But you can write some recommendations of books, magazines, etc */ /* ===== TAGS ===== Please use the syntax below for adding tags to this page, separated by spaces (use quotes for tags with spaces). The most important tags to be used are, separated by groups: - Statements | Commands | SpecialVariables | Operators {{tag>tagName}} */ {{tag>Functions Strings}}