Strings in Java.

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.
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 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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
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,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
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.
28-Jun-15 String and StringBuilder Part I: String.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
JAVA Programming (Session 5) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
10-2 Chapter 10 discusses the following main topics:  Introduction to Wrapper Classes  Character Testing and Conversion with the Character Class  More.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Text Processing and More about Wrapper Classes Starting.
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.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
String class. Method concat Create string object –> String st1, st2; Input character to string object –> st1=br.readLine(); st2= br.readLine(); Use method.
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:
String handling. when you create a String object, you are creating a string that cannot be changed. That is, once a String object has been created, you.
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.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Chapter 8 String Manipulation
The String class is defined in the java.lang package
Introduction to programming in java
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
String class in java Visit for more Learning Resources string.
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
String and StringBuilder
String Handling String, StringBuffer, StringBuilder
Java – String Handling.
Microsoft Visual Basic 2005: Reloaded Second Edition
String and StringBuilder
String methods 26-Apr-19.
Dr. Sampath Jayarathna Cal Poly Pomona
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

Strings in Java

Introduction String is a sequence of characters. In the Java programming language, strings are objects. Java provides a class called “String” in “java.lang” package to create and manipulate strings. The string value must be represented in “ ” (double cotes).

String greeting = “Good Morning!"; Class name (Data type) defined in Creating String Direct way to create a string Variable name String greeting = “Good Morning!"; Class name (Data type) defined in Java.lang Variable value

Creating String Another way to create a string Variable name String class Constructor String greeting = new String(“Good Morning!“); new operator Variable value Class name (Data type) defined in Java.lang

char msg[ ] = {‘’J,’A’,’V’,’A’}; String name = new String(msg); Creating String Another way to create a string char msg[ ] = {‘’J,’A’,’V’,’A’}; String name = new String(msg); Here, first we create a character array and pass that array as argument to the string class constructor

Operations in String Class length() concat() char charAt(int index)  int compareTo(String anotherString) int compareToIgnoreCase(String str) boolean endsWith(String suffix)  boolean equals(Object anObject) boolean equalsIgnoreCase(String anotherString)

Operations in String Class int indexOf(int ch)  int indexOf(int ch, int fromIndex)  int indexOf(String str) int lastIndexOf(int ch)  int lastIndexOf(int ch, int fromIndex)  int lastIndexOf(String str) boolean startsWith(String prefix, int toffset) String substring(int beginIndex, int endIndex) boolean matches(String regex) boolean startsWith(String prefix)

Operations in String Class String substring(int beginIndex) String toLowerCase() String toString() String toUpperCase()  String trim() 

stringVariable.length(); This method returns a integer which is equal to total number of Characters present in a string object. For example, the string length of "Note Book" is 9 including the space character. Syntax: stringVariable.length(); Example: greeting.length();

stringVariable1. concat(stringVariable2); greeting.concat(name); String concatenation is the operation of joining two string objects end to end. For example, the concatenation of two strings "Foot” and "ball” is "Football“  Syntax: stringVariable1. concat(stringVariable2); Example: greeting.concat(name);

String game = “Foot”+”ball”; concat() String concatenation can also be done using + operator For example, the concatenation of two strings "Foot” and "ball” is "Football“ as follows  String game = “Foot”+”ball”;

stringVariable. charAt(IndexValue); char charAt(int index)  This method takes an index as a parameter and returns the character that is present at that index of the string. Syntax: stringVariable. charAt(IndexValue); Example: greeting.charAt(2);

stringVariable1. equals(stringVariable2); greeting.equals(name); The equals is used to compare two Strings and states whether they are equal or not. Syntax: stringVariable1. equals(stringVariable2); Example: greeting.equals(name);

Syntax: Example: greeting.equalsIgnoreCase(name); The equals IgnoreCase is used to compare two Strings by ignoring letter case and states whether they are equal or not. Syntax: stringVariable1. equalgnoreCase(stringVariable2); Example: greeting.equalsIgnoreCase(name);

Syntax: Example: greeting.startsWith(name); The startsWith accepts a string and checks whether the invoking string starts with the given string or not. Syntax: stringVariable1. startsWith(stringVariable2); Example: greeting.startsWith(name);

Syntax: Example: greeting.startsWith(name,10); Note: startsWith method also have one more form as follows Syntax: stringVariable1. startsWith(stringVariable2,Index); Example: greeting.startsWith(name,10);

stringVariable1. endsWith(stringVariable2); greeting.endsWith(name); The endsWith accepts a string and determines whether the invoking string ends with the given string or not.  Syntax: stringVariable1. endsWith(stringVariable2); Example: greeting.endsWith(name);

Syntax: Searching Strings stringVariable.indexOf(charVariable); The String class provides 2 methods for searching a string.  indexOf() : Searches for the first occurrence of a character or substring. lastIndexOf() : Searches for the last occurrence of a character or substring Syntax: stringVariable.indexOf(charVariable); stringVariable.lastIndexOf(charVariable); stringVariable.indexOf(stringVariable); stringVariable.indexOf(charVariable, startIndex); stringVariable.lastIndexOf(charVariable, startIndex);

Syntax: substring - Modifying a String The substring method can be used to extract some characters from the String.  This method comes in two forms Syntax: stringVariable1 = stringVariable2.substring(index); stringVariable1 = stringVariable2.substring(index1, index2);

stringVariable.replace(originalChar, replcedChar); The replace() is used to replace a character or a sequence of characters of an invoking string with a desired character or set of characters  Syntax: stringVariable.replace(originalChar, replcedChar);

stringVariable.trim(); trim is used to trim/remove whitespace from the beginning and the end of the string that invoked this method. Syntax: stringVariable.trim();