Strings in Java Strings in Java are also a reference type.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
1 Strings and String Operations Overview l Creating String Objects l Substring methods l The Concatenation Operator l Strings are Immutable l Other Methods.
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Strings.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
© 2006 Pearson Addison-Wesley. All rights reserved char and String char is for storing single characters primitive type constants: a printable character.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
String and StringBuffer classes
char is for storing single characters
Strings, StringBuilder, and Character
Strings.
String class.
Programming in Java Text Books :
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Chapter 9 Strings and Text I/O
Lecture 10 Strings CSE /26/2018.
Lecture 07 String Jaeki Song.
Control Structure Chapter 3.
String and StringBuilder
CS2011 Introduction to Programming I Strings
String Methods.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Control Structure.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

Strings in Java Strings in Java are also a reference type. They are similar to an array of characters in that they represent an ordered sequence of characters with positions numbered from 0. String constants are enclosed in double quotes. Example: String message = "Hello World!"; System.out.println( message ); The class String provides many useful methods.

Comparison with character arrays Although a String has many similarities to an array of characters, there are some important differences. You don’t need to use new to create a string unless you want to use one of the String class constructors String s1 = "Hello!"; String s2 = new String("Hello!"); You DO NOT use [] to access the characters in the string. The contents of a String cannot be changed after it has been created. You can have a String variable refer to a different string, but you cannot change characters within the original string. There is NO equivalent to: char[] x = new char[] {'h','e','l','l','o'}; x[2] = 'q';

Conversion to/from character arrays To convert from a character array to a String: char[] x = new char[] {'h','e','l','l','o'}; String s1 = new String( x ); To convert from a String to a character array: char[] x = aString.toCharArray( );

Useful String methods Suppose we have String message = "Hello World!"; To find the length of a string: int theStringLength = message.length(); To find the character at position i (numbered from 0): int i = 4; char theChar = message.charAt( i );

Useful String methods To change any primitive data type to a String : int anInteger = 17; String aString = String.valueOf( anInteger ); To append one string after another (concatenation): String joinedString = string1 + string2;

Useful String methods To find a particular character or String within a String : First occurrence of 'x' int location = aString.indexOf('x'); First occurrence of "world" starting from position pos: int location2 = aString.indexOf( "world", pos ); There is also a corresponding method lastIndexOf( ) occurrence of "world" starting from position pos:

Useful String methods Substrings: The substring method returns a String consisting of the characters starting from the first position inclusive up to but NOT including the second position: String str = "abcdefghijklmno"; String str2 = str.substring( 2, 5 ); Result is "cde“ Case changes toLowerCase( ), toUpperCase( )

Comparing Strings A String is a reference type and so they are NOT compared with ==. The String class has a method compareTo() to compare 2 strings. The characters in each string are compared one at a time from left to right, using the collating sequence. The comparison stops after a character comparison results in a mismatch, or one string ends before the other. If str1 < str2, then compareTo() returns an int < 0 If str1 > str2, then compareTo() returns an int > 0 If the character at every index matches, and the strings are the same length, the method returns 0

Comparing Strings What is the value of result for these examples? String str1 = "abcde" ; String str2 = "abcfg" ; int result = str1.compareTo(str2); Example 2: String str2 = "ab" ;