GCSE Computing String Manipulation String Manipulation Why n




- Slides: 4

GCSE Computing: : String Manipulation

String Manipulation: Why? n Often in programming we need to manipulate strings especially if they are read in via a sequential file like a text document; n Consider a line in a text document where items are separated by commas: [name] , [date of birth] , [address] n n It might be useful to extract just one of the above; It might be useful to just extract the surname from the name segment. [Poor cartographer booklet] f

String Manipulation: In Practice n The following key functions can be used to manipulate strings: s. Name = “Grace Louise Carter” n Left( [string] , [length] ) returns the left side of the string: msgbox Left(s. Name, 5) would return “Grace” n Right( [string] , [length] ) returns the left side of the string: msgbox Right(s. Name, 6) would return “Carter” n Mid( [string] , [start] , [length] ) returns the left side of the string: msgbox Mid(s. Name, 7, 6) would return “Louise” n Len( [string]) returns the length of a string: msgbox Len(s. Name) would return 19 (includes spaces) n [string] & [string] concatenates (pushes together) two strings: msgbox Left(s. Name, 5) & Right(s. Name, 6) would return “Grace Carter” [Poor cartographer booklet] f

String Manipulation: In Practice n The following advanced functions can be used to manipulate strings: s. Name = “Grace, Louise, Carter, Female” n Instr( [start position] , [string to find] ) finds the position of a certain character within a string: i. First. Comma. Pos = Instr(1, s. Name, “, ”) would return 6. this can then be used to find the length of the first name in a list of names msgbox Left(s. Name, i. First. Comma. Pos) OR msgbox Left(s. Name, Instr(1, s. Name, “, ”) ) would return the “Grace” n This can be very effective when data is stored in arrays For i = 1 to 10 msgbox Left(s. Name(i), Instr(1, s. Name(i), “, ”) ) Next [Poor cartographer booklet] f