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 - [Link]
String s = "compsci";
String champ = new String("uilstate");
reference object
variable instantiation
© A+ Computer Science - [Link]
String s = "compsci";
s
0xF5 0xF5
"compsci"
A reference variable stores the
memory address of an object.
© A+ Computer Science - [Link]
String s = new String("compsci");
s
0xF5 0xF5
"compsci"
A reference variable stores the
memory address of an object.
© A+ Computer Science - [Link]
Methods provide / grant
access to an object’s
data / properties.
length( )
instance
substring( )
variables /
String data / indexOf( )
properties toString( )
© A+ Computer Science - [Link]
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 - [Link]
String s = "compsci";
int len = [Link](); OUTPUT
[Link]( len ); 7
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
Return methods perform some action
and return a result back.
.length() is a return method.
String s = "compsci";
int len = [Link]();
[Link]( len );
length() returns an integer back to the calling location.
The value returned is then assigned to variable len.
© A+ Computer Science - [Link]
String s = "compsci"; OUTPUT
[Link]([Link](0) + " "); cmi
[Link]([Link](2) + " ");
[Link]([Link](6));
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
String s = "compsci";
String sub ="";
OUTPUT
psci
sub = [Link](3); com
[Link](sub); sci
sub = [Link](0,3);
[Link](sub);
sub = [Link](4);
[Link](sub);
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
String s = "compsci";
String sub ="";
OUTPUT
mpsci
sub = [Link](2); mps
[Link](sub); sc
sub = [Link](2,5);
[Link](sub);
sub = [Link](4,6);
[Link](sub);
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
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 - [Link]
String s = "compsci";
int index = [Link]("mp"); OUTPUT
[Link](index); 2
0
index = [Link]("c");
-1
[Link](index);
index = [Link]('x');
[Link](index);
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
String s = "compsci";
int index = [Link]("pm"); OUTPUT
[Link](index); -1
5
index = [Link]('c');
1
[Link](index);
index = [Link]("omp");
[Link](index);
0 1 2 3 4 5 6
s c o m p s c i
© A+ Computer Science - [Link]
String one = "computer";
String two = "-sci";
String s = [Link](0,4) + two;
[Link](s);
OUTPUT
[Link]([Link]());
comp-sci
8
Concatenate is the process of combining
strings together to make a new string.
© A+ Computer Science - [Link]
Object Methods
String Processing
String Processing
Errors
String Processing
String Processing