Strings in Java. What data types have we seen so far?

Slides:



Advertisements
Similar presentations
Java
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Strings in Java 1. strings in java are handled by two classes String &
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.
Java Strings in 10 minutes
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Programming 2 CS112- Lab 2 Java
CIS 234: Strings. What Is a String? series of characters enclosed in double quotes characters can include letters (lower case and upper case), digits,
Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
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.
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.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
CIS 234: Strings (click, scroll down)Strings Dr. Ralph D. Westfall April, 2010.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
CSE 114 – Computer Science I Strings, I/O, and Methods Bonneville Salt Flats, Utah.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
From C++ to Java A whirlwind tour of Java for C++ programmers.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
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.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
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:
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Strings A String is a sequence of letters
Introduction to programming in java
String and StringBuffer classes
Java String Methods - Codehs
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
Documentation Need to have documentation in all programs
Programming in Java Text Books :
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
An Introduction to Java – Part I, language basics
Coding Concepts (Data- Types)
CS2011 Introduction to Programming I Strings
Strings in Java.
In Java, strings are objects that belong to class java.lang.String .
Pre-AP® Computer Science Quiz
Presentation transcript:

Strings in Java

What data types have we seen so far?

Data types we’ve seen so far… integer types: integer types: –int –(also short, char, and byte) real numbers: real numbers: –double and float

Introducing the String String String –Case sensitive (capital S is required) Declaring a String Declaring a String –How did we declare variables of the other types (such as int, double, float) in the past?

Declarations How have we already declared variables? How have we already declared variables? inti; floatf; doublex; So how do we declare Strings? So how do we declare Strings?

Declaring Strings //declare variables int beta; double delta; String sigma;

Assigning values to Strings //declare variables int beta; double delta; String sigma; //initialize variables beta = 1; delta = 2.0; sigma = "hello there";

Declaration w/ assignment //declare & init variables int beta = 1; double delta = 2.0; String sigma = "hello there";

String is a Java class Instances of class String are objects. Instances of class String are objects. We can print strings. We can print strings. String s = "hello"; System.out.println( s );

String is a Java class Instances of class String are objects. Instances of class String are objects. Useful String method: equality Useful String method: equality String s = "hello"; System.out.println( s.equals("Hello") );

String is a Java class (like input and output) Useful String method: equality Useful String method: equality String s = "hello"; System.out.println( "s is equal to hello is " + s.equals("Hello") ); s is equal to hellois false (boolean)

String methods equals() is case sensitive (i.e., it distinguishes between upper and lower case) equals() is case sensitive (i.e., it distinguishes between upper and lower case) –"hello" is not the same as "Hello" –(Note: Don’t use ==.)

String methods Wouldn’t it be nice to have a method that is like equals() but ignores case? Wouldn’t it be nice to have a method that is like equals() but ignores case? –"hello" is the same as "Hello" –What would be a good name for such a method?

String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") );

String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") ); s is equal to hellois true

String methods so far So far: So far: –equals() –equalsIgnoreCase()

More String methods length() length() startsWith() startsWith() endsWith() endsWith() toLowerCase() toLowerCase() toUpperCase() toUpperCase() charAt() charAt() substring() substring()

String Who defined the String class? Who defined the String class? Is there documentation for the String class? Is there documentation for the String class?

String Who defined the String class? Who defined the String class? –The Sun Microsystems company when it defined Java. Is there documentation for the String class? How many methods are there? Is there documentation for the String class? How many methods are there? –Lots! (over 75!) Lots!

The award for the longest name for a person belongs to a German immigrant to Philadelphia, Pennsylvania. The name he was given at birth, and which somehow fit on his passport was: (First and "middle" names) Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvim John Kenneth Loyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor Willian Xerxes Yancy Zeus (Last name) WolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaftschaferswesenchafewarenwholgepflegeundsorgfaltigkeitbeschutzenvonangereifenduchihrraubgiriigfeindewelchevorralternzwolftausendjahresvorandieerscheinenbanderersteerdeemmeshedrraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelshegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevanverstandigmenshlichkeittkonntevortpflanzenundsicherfreunanlebenslamdlichfreudeundruhemitnichteinfurchtvorangreifenvonandererintlligentgeschopfsvonhinzwischensternartigraumSenior

String method: length() String nm = "john jacob jingleheimer schmidt"; System.out.print( "Wow. " ); System.out.println( "You name is " + nm.length() + " long." ); What (type of thing) does length() return?

String method: length() //define first name String fn = "george"; //define last name String ln = "grevera"; int totalLength = fn.length() + ln.length();

More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1).

More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String str = "fred loves ethel"; System.out.println( str.charAt(1) );

More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String str = "fred loves ethel"; System.out.println( str.charAt(1) ); r

More string methods substring()substring()(Two of them.) substring() 1. String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). 2. String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0).

More string methods String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). String p = "fred loves ethel"; System.out.println( p.substring(1) );

More string methods String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). String p = "fred loves ethel"; System.out.println( p.substring(1) ); red loves ethel

More string methods String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). System.out.println( "fred loves ethel".substring(1) );

More string methods String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). System.out.println( "fred loves ethel".substring(1) ); red loves ethel

More string methods String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0). System.out.println( "fred loves ethel".substring(1,4) + "." );

More string methods String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0). System.out.println( "fred loves ethel".substring(1,4) + "." ); red.