0% found this document useful (0 votes)
9 views24 pages

Strings Intro

Uploaded by

Ximena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Strings Intro

Uploaded by

Ximena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Using Objects and String

Processing
Primitives, Objects, and String Processing
String s = "compsci";

0 1 2 3 4 5 6
s c o m p s c i

A string is a group of characters.


The first character in the group is at spot 0.

© A+ Computer Science - www.apluscompsci.com


String s = "compsci";
String champ = new String("uilstate");

reference object
variable instantiation

© A+ Computer Science - www.apluscompsci.com


String s = "compsci";

s
0xF5 0xF5

"compsci"

A reference variable stores the


memory address of an object.
© A+ Computer Science - www.apluscompsci.com
String s = new String("compsci");

s
0xF5 0xF5

"compsci"

A reference variable stores the


memory address of an object.
© A+ Computer Science - www.apluscompsci.com
Methods provide / grant
access to an object’s
data / properties.
length( )
instance
substring( )
variables /
String data / indexOf( )
properties toString( )

© A+ Computer Science - www.apluscompsci.com


String
frequently used methods

Name Use

substring(x,y) returns a section of the string from x to y


not including y
substring(x) returns a section of the string from x to
length-1
charAt(x) returns the char at spot x
length() returns the # of chars

© A+ Computer Science - www.apluscompsci.com


String s = "compsci";
int len = s.length(); OUTPUT
System.out.println( len ); 7

0 1 2 3 4 5 6

s c o m p s c i

© A+ Computer Science - www.apluscompsci.com


Return methods perform some action
and return a result back.
.length() is a return method.

String s = "compsci";
int len = s.length();
System.out.println( len );

length() returns an integer back to the calling location.


The value returned is then assigned to variable len.

© A+ Computer Science - www.apluscompsci.com


String s = "compsci"; OUTPUT

out.print(s.charAt(0) + " "); cmi


out.print(s.charAt(2) + " ");
out.println(s.charAt(6));

0 1 2 3 4 5 6

s c o m p s c i

© A+ Computer Science - www.apluscompsci.com


String s = "compsci";
String sub ="";
OUTPUT
psci
sub = s.substring(3); com
out.println(sub); sci

sub = s.substring(0,3);
out.println(sub);

sub = s.substring(4);
out.println(sub);
0 1 2 3 4 5 6

s c o m p s c i
© A+ Computer Science - www.apluscompsci.com
String s = "compsci";
String sub ="";
OUTPUT
mpsci
sub = s.substring(2); mps
out.println(sub); sc

sub = s.substring(2,5);
out.println(sub);

sub = s.substring(4,6);
out.println(sub);
0 1 2 3 4 5 6

s c o m p s c i
© A+ Computer Science - www.apluscompsci.com
String
frequently used methods

Name Use

indexOf( str ) returns the loc of String str in the string,


searching from spot 0 to spot length-1
indexOf( ch ) returns the loc of char ch in the string,
searching from spot 0 to spot length-1
lastIndexOf( str ) returns the loc of String str in the string,
searching from spot length-1 to spot 0
lastIndexOf( ch ) returns the loc of char ch in the string,
searching from spot length-1 to spot 0

© A+ Computer Science - www.apluscompsci.com


String s = "compsci";
int index = s.indexOf("mp"); OUTPUT
out.println(index); 2
0
index = s.indexOf("c");
-1
out.println(index);
index = s.indexOf('x');
out.println(index);
0 1 2 3 4 5 6

s c o m p s c i
© A+ Computer Science - www.apluscompsci.com
String s = "compsci";
int index = s.indexOf("pm"); OUTPUT
out.println(index); -1
5
index = s.lastIndexOf('c');
1
out.println(index);
index = s.lastIndexOf("omp");
out.println(index);
0 1 2 3 4 5 6

s c o m p s c i
© A+ Computer Science - www.apluscompsci.com
String one = "computer";
String two = "-sci";
String s = one.substring(0,4) + two;
out.println(s);
OUTPUT
out.println(s.length());
comp-sci
8
Concatenate is the process of combining
strings together to make a new string.

© A+ Computer Science - www.apluscompsci.com


Object Methods
String Processing
String Processing
Errors
String Processing
String Processing

You might also like