Chapter 8 String Manipulation

Slides:



Advertisements
Similar presentations
Java
Advertisements

String Pemrograman Berbasis Obyek Oleh Tita Karlita.
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.
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
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.
©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 ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
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 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.
28-Jun-15 String and StringBuilder Part I: String.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
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.
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.
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.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Strings A String is a sequence of letters
The String class is defined in the java.lang package
String and StringBuffer classes
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
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
String and StringBuilder
F4105 JAVA PROGRAMMING F4105 Java Programming
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
Chapter 9 Strings and Text I/O
Java – String Handling.
String and StringBuilder
String methods 26-Apr-19.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Chapter 8 String Manipulation

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating Strings: The most direct way to create a string is to write: String greeting = "Hello world!";

String Length: .length() method, returns the number of characters contained in the string object. public class StringDemo { public static void main(String args[]) { String p = "Dot saw I was Tod"; int len = p.length(); System.out.println( "String Length is : " + len ); } } Result: String Length is : 17

Concatenating Strings: The String class includes a method for concatenating two strings: string1.concat(string2); This returns a new string that is string1 with string2 added to it at the end. You can also use the concat() method with string literals, as in: "My name is ".concat("Zara"); Strings are more commonly concatenated with the + operator, as in: "Hello," + " world" + "!" which results in: "Hello, world!"

Example public class StringDemo { public static void main(String args[]) { String string1 = "saw I was "; System.out.println("Dot " + string1 + "Tod"); } This would produce the following result: Dot saw I was Tod

CharAt() Method Syntax: public char charAt(int index) index -- Index of the character to be returned. Example: public class Test { public static void main(String args[]) { String s = "Strings are immutable"; char result = s.charAt(8); System.out.println(result); } } This produces the following result: a

endsWith() Method public class Test{ public static void main(String args[]) { String Str = new String("This is really not immutable!!"); boolean retVal; retVal = Str.endsWith( "immutable!!" ); System.out.println("Returned Value = " + retVal ); retVal = Str.endsWith( "immu" ); System.out.println("Returned Value = " + retVal ); } } This produces the following result: Returned Value = true Returned Value = false

indexOf() Method Description: public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur. public int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the specified character. int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring.

Syntax public int indexOf(int ch ) or public int indexOf(int ch, int fromIndex) int indexOf(String str) int indexOf(String str, int fromIndex)

Example import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); String SubStr1 = new String("Tutorials"); String SubStr2 = new String("Sutorials"); System.out.print("Found Index :" ); System.out.println(Str.indexOf( 'o' )); System.out.println(Str.indexOf( 'o', 5 )); System.out.println( Str.indexOf( SubStr1 )); System.out.println( Str.indexOf( SubStr1, 15 )); System.out.println(Str.indexOf( SubStr2 )); } }

This produces the following result: Found Index :4 Found Index :9 Found Index :11 Found Index :-1

replace() Method Syntax: public String replace(char oldChar, char newChar) Parameters: Here is the detail of parameters: oldChar -- the old character. newChar -- the new character. Return Value: It returns a string derived from this string by replacing every occurrence of oldChar with newChar.

Example import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.replace('o', 'T')); System.out.print("Return Value :" ); System.out.println(Str.replace('l', 'D')); } } Result: WelcTme tT TutTrialspTint.cTm WeDcome to TutoriaDspoint.com

toLowerCase() Method Example import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :"); System.out.println(Str.toLowerCase()); } }

toUpperCase() Method Example import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String("Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.toUpperCase() ); } }

Java - String trim() Method Syntax: public String trim() Example import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String(" Welcome to Tutorialspoint.com "); System.out.print("Return Value :" ); System.out.println(Str.trim() ); } }