strings & arrays

17
Strings & Arrays CST200 Week 1 Instructor: Andreea Molnar

Upload: andreeamolnar

Post on 21-Jul-2015

2.420 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Strings & arrays

Strings & Arrays

CST200 – Week 1

Instructor: Andreea Molnar

Page 2: Strings & arrays

Outline

• Strings

• Arrays

Page 3: Strings & arrays

Strings and Arrays

The first letter of a String is at the

position 0 and the first value of an array

is at the position 0.

Page 4: Strings & arrays

Strings

Any sequence of characters, for example:

String s = “Arizona”;

String s = “1457 Mesa”;

String s = “@u”;

Page 5: Strings & arrays

Escape Sequences

Escape sequence Meaning

\b backspace

\t tab

\n newline

\r return

\’’ double quote

\’ single quote

\\ backslash

Page 6: Strings & arrays

Escape SequencesString firstName = "Mary";

String lastName = "Smith";

System.out.println(firstName + "\t" + lastName);

Mary Smith

System.out.println(firstName + "\n" + lastName);

Mary

Smith

System.out.println(firstName + "\"" + lastName);

Mary"Smith

System.out.println(firstName + "\\" + lastName);

Mary\Smith

Page 7: Strings & arrays

Strings - Methods

You can find all the String methods at:

http://docs.oracle.com/javase/7/docs/api/j

ava/lang/String.html

Page 8: Strings & arrays

Strings - Methods

public int length() – returns the length of the

string

String s = “CST 200”;

s.length() – will return 7 as there are 7

characters in the string

Page 9: Strings & arrays

Strings - Methods

public int length() – returns the length of the

string

String s = “CST 200”;

s.length() – will return 7 as there are 7

characters in the string (space is counted

as a separate character)

Page 10: Strings & arrays

Strings - Methods

public char charAt(int index)– returns the

character value at the specified index

String s = “CST 200”;

C S T 2 0 0

0 1 2 3 4 5 6

index

Page 11: Strings & arrays

Strings - Methods

public char charAt(int index)– returns the

character value at the specified index

String s = “CST 200”;

C S T 2 0 0

0 1 2 3 4 5 6

The first character of a String is at the

position 0!!!

Page 12: Strings & arrays

Strings - Methods

String s = “CST 200”;

C S T 2 0 0

0 1 2 3 4 5 6

System.out.println(s.charAt(0));//will print C

System.out.println(s.charAt(6));//will print 0

Page 13: Strings & arrays

Strings - Methods

String indexOf (String str)

returns the index of the first occurrence of str

if multiple occurrences returns just the first one

String s = “CST 200”;

C S T 2 0 0

0 1 2 3 4 5 6

System.out.println(s.indexOf(“T”));

//will print 2

Page 14: Strings & arrays

String Methods

String substring(int beginIndex, int endIndex)

returns a substring that begins at the

beginIndex and ends at the endIndex -1

String str = “Arizona State University”;

str.substring (3, 5); //substring that begins at the index 3

(z) and ends at the 5-1 (o) zo

Page 15: Strings & arrays

Arrays

Array is a container that holds a fixed

number of values.

int[] array = {10, 20, 30, 50};

10 20 30 50

0 1 2 3

index

Page 16: Strings & arrays

Arraysint[] array = {10, 20, 30, 50};

10 20 30 50

0 1 2 3

To print the values of this array:

System.out.println(array[0]);//will print 10

System.out.println(array[1]);//will print 20

System.out.println(array[2]);//will print 30

System.out.println(array[3]);//will print 50

Page 17: Strings & arrays

Summary

• The first character of a String starts at

position 0

• The first element of an array starts at

the position 0