25
Strings : Getting Stuff Done

Strings : Getting Stuff Done

Embed Size (px)

DESCRIPTION

Strings : Getting Stuff Done. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Strings : Getting Stuff Done

Strings : Getting Stuff Done

Page 2: Strings : Getting Stuff Done

Basic Recipes

• Does this look like an email address?

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 3: Strings : Getting Stuff Done

Basic Recipes

• Does this look like an email address?– Is there an @

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 4: Strings : Getting Stuff Done

What is that Warning???

• Ugly digression…

Page 5: Strings : Getting Stuff Done

Basic Recipes

• Does this string have a @ in it?– Happier compiler version

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 6: Strings : Getting Stuff Done

Char vs 1 char string

• 'a' is not the same kind of thing as "a"

Number Object

Page 7: Strings : Getting Stuff Done

Char vs 1 char string

• string.at(index) and string[index] give a char

• No

• Yes

Page 8: Strings : Getting Stuff Done

Char vs 1 char string

• All other string functions give string

• Yes

• No

Page 9: Strings : Getting Stuff Done

Basic Recipes

• Want to separate username and domain:

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 10: Strings : Getting Stuff Done

Basic Recipes

• Method 2:

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 11: Strings : Getting Stuff Done

Basic Recipes

• I want to replace a @ with #:

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 12: Strings : Getting Stuff Done

Basic Recipes

• I want to replace a @ with #:– Shorter version

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Set one char in stringto new value… must be a char

Page 13: Strings : Getting Stuff Done

Basic Recipes

• I want to replace a @ with #:– Long version

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 14: Strings : Getting Stuff Done

Chopping

• Generally easier to chop as you go:

Page 15: Strings : Getting Stuff Done

Chopping

• Finding both '-' and then chopping:

Page 16: Strings : Getting Stuff Done

Looping

• Finding all groups in string like:123-4324-23-34-…-234

• While there is another dash, find dash, print everything up to dash

Page 17: Strings : Getting Stuff Done

Looping

• By chopping parts off:

Page 18: Strings : Getting Stuff Done

Looping

• By moving start/end indexes:

Page 19: Strings : Getting Stuff Done

Looping Characters

• Process each character : – Loop from 0 to length - 1

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Current positionCurrent letter

Page 20: Strings : Getting Stuff Done

Looping Characters

• Backwards

0 1 2 3 4 5 6 7 8

b o b @ a . c o m

Page 21: Strings : Getting Stuff Done

Modifying as we go

• Shift all chars by one:– Get current letter–Modify– Replace current letter with changed

Page 22: Strings : Getting Stuff Done

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 = 484112E916 = 484110

4096’s163

256’s162

16’s161

1’s160

1 2 E 9

Page 23: Strings : Getting Stuff Done

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

4096’s163

256’s162

16’s161

1’s160

1 2 E 9

Page 24: Strings : Getting Stuff Done

Case Study Hex Conversion

• Need to access each digit• Need to figure out its value– char '4' != 4– But '4' – '0' = 4

Page 25: Strings : Getting Stuff Done

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

4096’s163

256’s162

16’s161

1’s160

1 2 E 9