250 likes | 356 Vues
This guide explores essential string manipulation techniques, focusing on operations like replacing characters, extracting substrings, and converting hexadecimal numbers to decimal. It delves into methods for checking if a string contains an '@' symbol, splitting usernames from domains, and efficiently processing characters in loops. Additionally, it covers the intricacies of hex to decimal conversions, illustrating how to access individual digits and calculate their values based on position. This resource is perfect for beginners looking to enhance their programming skills in string handling.
E N D
Basic Recipes • Does this look like an email address?
Basic Recipes • Does this look like an email address? • Is there an @
What is that Warning??? • Ugly digression…
Basic Recipes • Does this string have a @ in it? • Happier compiler version
Char vs 1 char string • 'a' is not the same kind of thing as "a" Object Number
Char vs 1 char string • string.at(index) and string[index] give a char • No • Yes
Char vs 1 char string • All other string functions give string • Yes • No
Basic Recipes • Want to separate username and domain:
Basic Recipes • Method 2:
Basic Recipes • I want to replace a @ with #:
Basic Recipes • I want to replace a @ with #: • Shorter version Set one char in stringto new value… must be a char
Basic Recipes • I want to replace a @ with #: • Long version
Chopping • Generally easier to chop as you go:
Chopping • Finding both '-' and then chopping:
Looping • Finding all groups in string like:123-4324-23-34-…-234 • While there is another dash, find dash, print everything up to dash
Looping • By chopping parts off:
Looping • By moving start/end indexes:
Looping Characters • Process each character : • Loop from 0 to length - 1 Current position Current letter
Looping Characters • Backwards
Modifying as we go • Shift all chars by one: • Get current letter • Modify • Replace current letter with changed
Case Study Hex Conversion • Convert hex numbers to decimal: • Possible digits:0-9, A (10), B (11), C (12), D (13), E (14), F (15) • Place values powers of 16: 1 x 4096 + 2 x 256 + 14 x 16 + 9 x 1 = 4841 12E916 = 484110
Case Study Hex Conversion • Need to access each digit • Need to figure out its value • Need to keep track of what place we are in • Need to build up a total
Case Study Hex Conversion • Need to access each digit • Need to figure out its value • char '4' != 4 • But '4' – '0' = 4
Case Study Hex Conversion • Need to access each digit • Need to figure out its value • Need to keep track of what place we are in • Each column is 16 x bigger than one to right • Need to build up a total