CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,

Slides:



Advertisements
Similar presentations
Strings Testing for equality with strings.
Advertisements

Strings.
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.
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.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Constants and Data Types Constants Data Types Reading for this class: L&L,
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9: Characters * Character primitives * Character Wrapper class.
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 ,
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Fundamental Programming Structures in Java: Strings.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
The String Class. Objectives: Learn about literal strings Learn about String constructors Learn about commonly used methods Understand immutability of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
Primitive Variables.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Introduction to Java Java Translation Program Structure
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
Review Spatial Filters – Smooth – Blur – Low Pass Filter – Sharpen – High Pass Filter – Edge detection – Erosion – Dilation Other Pixel Filters – Thresholding.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Primitive Variables.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
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.
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
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:
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
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.
Strings A String is a sequence of letters
Java String Methods - Codehs
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Computers & Programming Languages
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
CS2011 Introduction to Programming I Strings
In Java, strings are objects that belong to class java.lang.String .
Pre-AP® Computer Science Quiz
Presentation transcript:

CIS 234: Strings

What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits, special characters (spaces, punctuation marks, mathematical operators, etc.) "Kim" "137 Flower St." "a + b"

Strings in Memory memory is assigned when a string is created if a string's value changes, its memory location (and possibly length) changes "anonymous" strings have a memory location but no variable name System.out.print("Total: "); //anonymous

Strings in Memory - 2 each character in a string can be identified by an offset from its memory location 1 st character is at address of string offset = 0 2 nd character is at string address + 1 offset = 2 bytes (Unicode) 3 rd is at address is at address + 2 x 2

String Class String is a class in Java, so strings are declared with String (1 st letter capitalized) unlike other classes, don't always have to use new String myName = new String("Ralph"); // ok String yourName = "Viji"; // also ok

String Class Methods when create a string, you create an object of the String class can use String class methods with strings you create, attaching a method to a string with a dot size = myName.length(); /*length method in String class assigns # of characters in myName object to size */

String Class Comparisons with objects, == compares memory locations, not actual values == wouldn't recognize same string value in different memory locations use String class methods to compare values

String Class equals Method compares strings in same or different memory locations aName = "Lee "; //includes spaces test = (aName.equals("Lee")) // false strings are not equal because "Lee " includes space characters

String Class compare Method compares two strings letter by letter if identical, returns 0 otherwise returns distance between 1 st nonmatching characters if string attached to method is alphabetically greater than argument, value is positive if less, value is negative

String compare Method - 2 aName = "Li"; distance = (aName.compare("Le")); // 4 // i is ASCII 105, e is ASCII 101 aName ("Li) is attached to compare method with a dot alphabetically, "Li" comes after "Lee" therefore distance is positive (+4)

String length Method size = my .length(); possible to have an array of strings length is also a method of Array class make sure that object you attach length method to is string, not an array of strings

String indexOf Method finds first location of a specific character within a string 1 st character has index of 0 if character not found, returns –1 (minus) String his = int at =

String charAt Method returns character at offset (integer) provided in argument twoName = "Lidia"; firstLetter = twoName.charAt(0); secondLetter = twoName.charAt(1);

Practice String twoName = "Lidia"; int num = twoName.length(); // num = ? int num2 = twoName.indexOf ('i'); //num2 = ? char myChar = twoName.charAt(4); //myChar = ?

startsWith, endsWith Methods compare start or end of string with another string value return true or false String aString = "vegetate"; boolean test1 = aString.startsWith("b"); boolean test2 = aString.endsWith("ate");

String replace Method replaces all occurrences of 1 character with another "global search and replace" arguments are character to find, character to replace it with String oneString = "some hot"; String twoString = oneString.replace('o', 'a'); // twoString = ?

String substring Method returns part of another string arguments are starting position (offset) and number of characters String oneString = "separate"; String twoString = oneString.substring(4, 3); // twoString = ?

String Class Case Methods toUpperCase converts to capital letters aName = aName.toUpperCase(); toLowerCase converts to lower case

Strings to Numbers use data type "wrapper" class methods wrapper classes make it possible to treat "primitive" data types like they were classes int num = Integer.parseInt("135"); Integer class is wrapper for int data type double dNum = Double.parseDouble("1.2343"); Double class is double data type wrapper