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.

Slides:



Advertisements
Similar presentations
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Advertisements

Java
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.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
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.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
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.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Lecture 14 The String Class Lecture 14 The String Class Instructor: Craig Duckett.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
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.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
Programming in Java Dr. M. Ahmadzadeh. Course Outline (subject to tiny changes) I will cover the following but not necessarily in this order. –Strings.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Introduction to Java Java Translation Program Structure
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
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.
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
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.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
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.
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:
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.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Chapter 8 String Manipulation
Strings A String is a sequence of letters
Introduction to programming in java
String class.
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
Tonga Institute of Higher Education
Lecture 07 String Jaeki Song.
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
Strings in Java.
Switch, Strings, and ArrayLists in Java
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Pre-AP® Computer Science Quiz
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

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 as a 6-element array ('M', 'a', 'r', 't', 'y', 'n'). The String class allows us to manipulate these data items.

Strings in java String is a class in Java Strings are constant (values cannot be changed after they are created) Set of characters "Lisa Simpson" is a string. "A" is a string, 'A' is a character. Note the difference here. Character literals are delimited by single quotes and String literals are delimited by double quotes.

Methods on Strings The String class provides methods to – Return the character at a specific index (charAt) – Return the index of a specific character (indexOf) – Compare two strings (compareTo) – Concatenate two strings (concat) – Check the length of a string (length) – Extract a sub-string (substring) – Replace all instances of a character with another character (replace)

How long is a String Use length() to find out how long a string is String test; test = new String("The quick, brown"); System.out.println("Length of test is: “ + test.length()); Works on anonymous strings too: System.out.println("Lisa Simpson is: “ + "Lisa Simpson".length() + " characters long.");

Finding characters in String at specific index Strings are character sequences Grab individual characters with charAt() public char charAt(int index) A quick example: String test = “Hello” for (int nextChar = 0;nextChar < test.length(); nextChar++) { System.out.println(test.charAt(nextChar)); } HelloHello Output H is the character at index 0, e at index 1 and so on

Finding index of particular character We can find index of characters using following two methods indexOf(int) lastIndexOf(int) Examples "abcdefabc".indexOf(‘b’) is 1 "abcdefabc".lastIndexOf(‘b’) is 7

Substrings A substring is a string within a string Substring method – public String substring (int beginIndex, int endIndex) Includes character at beginIndex, but not the character at endIndex. “Hello World".substring(3,8) is “lo Wo”.

Replace character with another character Replace one character with another in a new string replace (char oldChar, char newChar) Example: String name = “Hello World"; String new_name = name.replace(‘e', ‘a'); // new_name will be “Hallo World”

Case Conversion Make a copy of a string, all in one case Handy for enforcing consistency: always switch strings to upper case or lower case public String toLowerCase() public String toUpperCase()