arrays and strings - part 1

arrays and strings - part 1 - COP 3330: Object-Oriented...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3330: Arrays & Strings in Java Page 1 © Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2007 Arrays and Strings in Java – Part 1 School of Electrical Engineering and Computer Science University of Central Florida Instructor : Mark Llewellyn markl@cs.ucf.edu HEC 236, 823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2007
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Arrays & Strings in Java Page 2 © Mark Llewellyn Strings •A string is a sequence of characters. • Java provides two classes to support strings: – String class – the instances of String class are constants, i.e., the content of a String object cannot be changed after its creation. – StringBuffer class – the instances of StringBuffer class are mutable, i.e., the content of a StringBuffer object can be changed after its creation. • The String class has some unique privileges not shared by ordinary classes. – A string can be created using string literals. – Operator + can be applicable to strings. • The characters in a string are indexed by 0 to n-1, where n is the length of the string.
Background image of page 2
COP 3330: Arrays & Strings in Java Page 3 © Mark Llewellyn String Class • Creation of a String object. String s = new String(“abc”); String s = “abc”; • Add operator: String s1 = “abc”; String s2 = “de”; String s3 = s1 + s2; //s3 = “abcde” •T h e String class has a lot of methods. These methods are useful to manipulate strings.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Arrays & Strings in Java Page 4 © Mark Llewellyn length and charAt methods int length() – this instance method returns the length of the string. String s=“abc”; s.length() Î returns 3 char charAt(int index) – this instance method returns the character at a specific index. String s=“abcde”; s.length(); Î 5 s.charAt(0); Î ‘a’ s.charAt(1); Î ‘b’ s.charAt(4); Î ‘e’ s.charAt(5); Î error
Background image of page 4
COP 3330: Arrays & Strings in Java Page 5 © Mark Llewellyn indexOf method int indexOf(char c) int indexOf(char c, int index) Returns the index of the first occurrence of the character c in the current object starting at position index (default 0). Returns –1 if there is no such occurrence. [overloaded method] String s=“ababc”; s.indexOf(‘b’);//returns 1 s.indexOf(‘b’,2); //returns 3 s.indexOf(‘d’,2); //returns -1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Arrays & Strings in Java Page 6 © Mark Llewellyn indexOf method (cont.) int indexOf(String s2) int indexOf(String s2, int index) Returns the index of the first occurrence of the substring s2 in the current object, beginning at position index (default 0). Returns –1 if there is no such occurrence. String s=“daabcabc”; String s2=“ab”; s.indexOf(s2); //returns 2 s.indexOf(s2,3); //returns 5 s.indexOf(s2,6); //returns -1
Background image of page 6
COP 3330: Arrays & Strings in Java Page 7 © Mark Llewellyn substring method String substring(int startindex) String substring(int startindex, int lastindex) Returns the substring of the current object starting from startindex and ending with lastindex-1 (or the last index of the string if lastindex is not given). [overloaded method] String s=“abcdef”; s.substring(1); //returns “bcdef” s.substring(3); //returns “def” s.substring(1,4);//returns “bcd” s.substring(3,5);//returns “de”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/03/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 72

arrays and strings - part 1 - COP 3330: Object-Oriented...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online