String. Other languages that implement strings as character arrays

Similar documents
Lab 14 & 15: String Handling

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

STRINGS AND STRINGBUILDERS. Spring 2019

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Class Library java.lang Package. Bok, Jong Soon

- Thus there is a String class (a large class)

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

String related classes

Creating Strings. String Length


CST242 Strings and Characters Page 1

ECS-503 Object Oriented Techniques

Class. Chapter 6: Data Abstraction. Example. Class

Chapter 12 Strings and Characters. Dr. Hikmat Jaber

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

5/23/2015. Core Java Syllabus. VikRam ShaRma

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Introductory Mobile Application Development

CS251L REVIEW Derek Trumbo UNM

Java Foundations: Unit 3. Parts of a Java Program

CHAPTER 6 MOST COMMONLY USED LIBRARIES

appreciate the difference between a char and a string understand and use the String class methods

Index COPYRIGHTED MATERIAL

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

"Hello" " This " + "is String " + "concatenation"

TEST (MODULE:- 1 and 2)

Programming with Java

USING LIBRARY CLASSES

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Programming Techniques

C08c: A Few Classes in the Java Library (II)

Chapter 8 Strings. Chapter 8 Strings

STUDENT LESSON A10 The String Class

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

CS112 Lecture: Characters and Strings

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Building Strings and Exploring String Class:

Java String Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace,

Lecture Notes K.Yellaswamy Assistant Professor K L University

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Eng. Mohammed Abdualal

CS 1301 Ch 8, Part A

Chapter 10: Text Processing and More about Wrapper Classes

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Ohad Barzilay and Oranit Dror

More on variables and methods

H212 Introduction to Software Systems Honors

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

APPENDIX A: SUMMARY OF IMPORTANT JAVA FEATURES

Welcome to the Using Objects lab!

TEXT-BASED APPLICATIONS

Remote Monitoring Example Remote RF Power Output Monitor

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

Selected Questions from by Nageshwara Rao

More non-primitive types Lesson 06

A couple of decent C++ web resources you might want to bookmark:

Strings, Strings and characters, String class methods. JAVA Standard Edition

Class. Chapter 6: Data Abstraction. Example. Class

Object-Oriented Programming

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

Lesson:9 Working with Array and String

Contents. Osp::Base::Object Osp::Base::String Osp::Base::Buffer Osp::Base::DateTime

Q-2 Why java is known as Platform-independent language?

SOFTWARE DEVELOPMENT 1. Strings and Enumerations 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Lecture Notes for CS 150 Fall 2009; Version 0.5

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Chapter 8: Strings and Things

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Building Java Programs

Tha Java Programming Language

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

BM214E Object Oriented Programming Lecture 8

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Building Java Programs

Introduction to Computer Programming

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

COMP 110/L Lecture 13. Kyle Dewey

CS 106 Introduction to Computer Science I

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Enough java.lang.string to Hang Ourselves...

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Prashanth Kumar K(Head-Dept of Computers)

Welcome to the Using Objects lab!

Lab 3: Call to Order CSCI 2101 Fall 2018

Transcription:

1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14. lastindexof() 15. substring() 16. concat() 17. replace() String 18. trim() 19. valueof() 20. tolowercase() 21. touppercase() 22. capacity() 23. ensurecapacity() 24. setlength() 25. setcharat() 26. getchars() 27. append() 28. insert() 29. reverse() 30. delete() 31. deletecharat() 32. replace() 33. substring() 1

String is a sequence of characters. String Other languages that implement strings as character arrays Java implements strings as objects of type String. Once a String object has been created, you cannot change the characters that comprise that string Java provides two options: StringBuffer and StringBuilder. strings that can be modified after they are created. Both hold The strings within objects of type String are unchangeable means that the contents of the String instance cannot be changed after it has been created. A variable declared as a String reference can be changed to point at some other String object at any time. 2

To create an empty String The String Constructors String s = new String(); you will want to create strings that have initial values char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); String(char chars[ ]) You can specify a subrange of a character array as an initializer using the following constructor: String(char chars[ ], int startindex, int numberofchars) char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; String s = new String(chars, 2, 3); 3

You can construct a String object that contains the same character sequence as another String object using this constructor: String(String strobj) 4

5

String Length int length( ) char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System.out.println(s.length()); Special String Operations The automatic creation of new String instances from string literals Concatenation of multiple String objects by use of the + operator, The conversion of other data types to a string representation. There are explicit methods available to perform all of these functions, but Java does them automatically as a convenience for the programmer 6

String Literals char chars[] = { 'a', 'b', 'c' }; String s1 = new String(chars); String s2 = "abc"; you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. System.out.println("abc".length()); String Concatenation String age = "9"; String s = "He is " + age + " years old."; System.out.println(s); 7

String Concatenation with Other Data Types int age = 9; String s = "He is " + age + " years old."; System.out.println(s); String s = "four: " + 2 + 2; System.out.println(s); This fragment displays four: 22 rather than the four: 4 To complete the integer addition first, you must use parentheses, like this: String s = "four: " + (2 + 2); Now s contains the string four: 4. 8

String Conversion and tostring( ) Every class implements tostring() because it is defined by Object tostring() can be overridden General form String tostring() 9

String Conversion and tostring( ) // Override tostring() for Box class. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } public String tostring() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; } } class tostringdemo { public static void main(string args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System.out.println(b); // convert Box to string System.out.println(s); } } 10

String Conversion and tostring( ) // Override tostring() for Box class. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } public String tostring() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; } } class tostringdemo { public static void main(string args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System.out.println(b); // convert Box to string System.out.println(s); } } Output: Dimensions are 10.0 by 14.0 by 12.0. Box b: Dimensions are 10.0 by 14.0 by 12.0. 11

12

Converts all characters in a String object into a character array. 13

14

true false false true 15

Compares a specific region inside a string with another region in another string 16

true true 17

== compares two objects references to see whether they refer to the same instance 18

Output Hello equals Hello true Hello == Hello false 19

20

21

Int indexof(char ch) Int lastindexof(char ch) 22

23

7 65 7 55 11 55 44 55 24

It extracts a substring public class StringDemo2{ public static void main(string args[]){ String s1="my India"; System.out.println("indexOf(ia)"+s1.indexOf("ia")); System.out.println("substring(0,5)"+s1.substring(0,5)); } } 25

It extracts a substring public class StringDemo2{ public static void main(string args[]){ String s1="my India"; System.out.println("indexOf(ia)"+s1.indexOf("ia")); System.out.println("substring(0,5)"+s1.substring(0,5)); } } Output: indexof(ia)6 substring(0,5)my In 26

It extracts a substring Home Work 27

It extracts a substring Home Work 28

Replaces all occurrences of character with other in the invoking string 29

Replaces all occurrences of character with other in the invoking string Output: Hewwo 30

class demo { public static void main(string args[]) { String s="hello".replace("lo","ooll"); System.out.println(s); } } 31

class demo { public static void main(string args[]) { String s="hello".replace("lo","ooll"); System.out.println(s); } } Output: Helooll 32

Removes all leading and trailing whitespaces. 33

Removes all leading and trailing whitespaces. Output: Hello World 34

35

36

import java.util.scanner; public class StringDemo2{ public static void main(string args[]){ Scanner in=new Scanner(System.in); String s2=new String(); System.out.println("valueOf(5.5)"+s2.valueOf(5.5)); } } 37

import java.util.scanner; public class StringDemo2{ public static void main(string args[]){ Scanner in=new Scanner(System.in); String s2=new String(); System.out.println("valueOf(5.5)"+s2.valueOf(5.5)); } } valueof(5.5)5.5 38

39

40

41

String represents fixed length, immutable characters sequences, whereas, StringBuffer represents grow able and writable character sequences, it automatically grows to accommodate. 42

Total characters present Total size of variable 43

Total characters present Total size of variable 5 21 44

It sets capacity in advance A single character can be obtained 45

46

Buffer = Hello charat(1) = e New Buffer = Hi charat(1) = i 47

48

a=42! 49

50

insert(2,"like")= I LikeJava 51

52

53

Output: After delete:this a test. After deletecharat:his a test. 54

class demo { public static void main(string args[]) { StringBuffer sb=new StringBuffer("This is a test."); sb.replace(4,7,"kk"); System.out.println("After replace:"+sb); } } 55

class demo { public static void main(string args[]) { StringBuffer sb=new StringBuffer("This is a test."); sb.replace(4,7,"kk"); System.out.println("After replace:"+sb); } } Output: After replace:thiskk a test. 56

57

58