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. Every time.

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
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 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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
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.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Fundamental Programming Structures in Java: Strings.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 8 Characters and Strings.
©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  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Characters, String and Regular expressions. Characters char data type is used to represent a single character. Characters are stored in a computer memory.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
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.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC Programming I Lecture 9 September 11, 2002.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
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:
17-Feb-16 String and StringBuilder Part I: String.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
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,
The Methods and What You Need to Know for the AP Exam
Strings A String is a sequence of letters
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
Array, Strings and Vectors
String class.
EKT 472: Object Oriented Programming
Java Review: Reference Types
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
String Objects & its Methods
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
Week 9 – Lesson 1 Arrays – Character Strings
Strings A string is a sequence of characters that is treated as a single value. We have been using strings all along. For example Blotgrunge ... out to.
Object Oriented Programming in java
String and StringBuilder
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
String Class.
In Java, strings are objects that belong to class java.lang.String .
String Objects & its Methods
Presentation transcript:

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. Every time you use a “” string, a string object is created automatically. For example, to display text Blotgrunge ... out to reality ... HelloWorld.java String is a class in the java.lang package. System.out.println("Hello, how are you?"); We have also seen the string concatenation operation. For example, the following code reads in a person’s name and greets the user: //assume inputBox and messageBox are declared //and initialized correctly String name; name = inputBox.getString("What is your name?"); messageBox.show("Hello, " + name + ". Nice to meet you.");

Explicit String Objects A declaration and object creation are needed for instances of the String class. For example, String name1,name2,name3; name1 = "Frooot"; name2 = new String(name1); name3 = new String({’F’,’r’,’o’,’o’,’t’}); Blitititi … out to reality … StringVar.java Blitititi … out to reality … StringVar2.java

Concatenating Strings The + operator concatenates strings. A new string object is created - the operands are not affected. Old strings are garbage collected. Gadzook … out to reality … StringCat.java Note that System.out.print really does this, not “and also print”. Gadzook … back to reality … StringVar.java Gadzook … back to reality … StringVar2.java

String variables are References - 1 Code String word1, word2; word1 = "Java”; word2 = word1; A Both word1 and word2 are allocated memory (to store references), but the objects themselves are not yet created, so they both contain null. word1 L word2 State of Memory L After is executed A

String variables are References - 2 Code String word1, word2; word1 = "Java”; word2 = word1; B One String object is created and assigned to word1, so word1 contains the address of this object. word1 word2 word1 String Java word2 L State of Memory After is executed B

String variables are References - 3 Content of word1, which is an address, is assigned to word2, making word2 refer to the same object. Code String word1, word2; word1 = "Java”; word2 = word1; C word1 word2 String Java After is executed C State of Memory Gadzook … out to reality … StringAlias.java Gadzook … out to reality … StringAlias2.java

Equality (==) vs. equals – Case 1 String Java word1 String Java word2 word1 and word2 point to different objects having the same string. false word1 == word2 word1.equals( word2 ) true

Equality (==) vs. equals – Case 2 String Java word1 String Bali word2 word1 and word2 point to different objects with different strings. false word1 == word2 word1.equals( word2 ) false

Equality (==) vs. equals - Case 3 word1 word2 String Java word1 and word2 point to the same object. true word1 == word2 word1.equals( word2 ) true Quixote … out to reality … StringJavas.java

Determining the Size 7 Error! We determine the number of characters in a String with the length method. String name = "Sumatra"; String str2 = ""; String str3; name.length(); 7 str2.length(); Error because no object is created for str3, so it is a null. Remember that a String is an object. If we simply declare, for example, String str3; and forget to create a String object, then str3 is a null object. You will get a NullPointerException if you try to a method of non-existent object, such as str3 in this example. str3.length(); Error!

Accessing Individual Elements Individual characters in a String accessed with the charAt method. String name = “Sumatra”; 1 2 3 4 5 6 S u m a t r name name.charAt( 3 ) Olipidoo … out to reality … StringVowels.java Olipidoo … out to reality … StringWords.java

Other Useful String Operators Method Meaning compareTo Compares the two strings. str1.compareTo( str2 ) substring Extracts the a substring from a string. str1.substring( 1, 4 ) trim Removes the leading and trailing spaces. str1.trim( ) valueOf Converts a given primitive data value to a string. String.valueOf( 123.4565 ) startsWith Returns true if a string starts with a specified prefix string. str1.startsWith( str2 ) endsWith Returns true if a string ends with a specified suffix string. str1.endsWith( str2 ) Here are some examples: String str1 = “Java”, str2 = “ Wow “; str1.compareTo( “Hello” ); //returns positive integer //because str1 >= “Hello” str1.substring( 1, 4 ); //returns “ava” str2.trim( ) //returns “Wow”, str2 stays same str1.startsWith( “Ja” ); //returns true str1.endsWith( “avi” ); //returns false

Command line Strings The formal arguments to the main method receive strings from the command line arguments. When running a program, supply command line arguments after the program name, e.g., java MyJavaProgram cat 27 'Java is great!' has three command line arguments. Gadzook … out to reality … MyJavaProgram.java When we write String name; we must say “name is a variable of type String whose value is a reference to an instance of String” to be precise. However, when the value of a variable X is a reference to an instance of class Y, we usually say “X is an instance of Y” or “X is a Y object.” For example, we say canvas is a DrawingBoard object

Strings and Methods Strings are reference variables, so (like arrays) formal parameters point to the actual parameter data Strings returned from methods point to the data created in the method Likidylik … out to reality … StringMethod.java

Strings are Immutable A String object is immutable, which means that once a String object is created, is cannot be changed. It is not possible to add, delete, or modify characters of a String object. The methods of the String class, such as toUpperCase and substring, do not modify the original string; they return a new string. Java adopts this immutability restriction to implement an efficient memory allocation scheme for managing String objects. Oieeeeiooo … out to reality … StringImmut.java

StringBuffer Creating a new string from the old one will work for most cases, but sometimes manipulating the content of a string directly is more convenient. Manipulation means operations such as replacing a character, appending a string with another string, deleting a portion of a string, and so forth. Java has a StringBuffer class for this. StringBuffers are created from strings (no shorthand) Ay … out to reality … StringBufferX.java Ay … out to reality … StringBufferMake.java