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

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

Creating Strings. String Length

Getting started with Java

Eng. Mohammed Abdualal

Object Oriented Software Design

Object Oriented Software Design

Data Types. Lecture2: Java Basics. Wrapper Class. Primitive data types. Bohyung Han CSE, POSTECH

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

An overview of Java, Data types and variables

Java Basic Datatypees

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

JAVA NUMBERS, CHARS AND STRINGS

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

Basics of Java Programming

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

We now start exploring some key elements of the Java programming language and ways of performing I/O

B.V. Patel Institute of BMC & IT, UTU 2014

A variable is a name for a location in memory A variable must be declared

Chapter 6 Primitive types

CMPT 125: Lecture 3 Data and Expressions

Table of Contents Date(s) Title/Topic Page #s. Abstraction

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 02: Using Data

Chapter 2: Using Data

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

4 Programming Fundamentals. Introduction to Programming 1 1

Introduction to Programming Using Java (98-388)

Chapter 2 Elementary Programming

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

Preview from Notesale.co.uk Page 9 of 108

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS 106 Introduction to Computer Science I

Chapter 2, Part I Introduction to C Programming

Chapter 2: Data and Expressions

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Fundamentals of Programming. By Budditha Hettige

Java Programming Language Mr.Rungrote Phonkam

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Chapter. Let's explore some other fundamental programming concepts

VARIABLES, DATA TYPES,

Variables, Constants, and Data Types

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

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

2 nd Week Lecture Notes

2 rd class Department of Programming. OOP with Java Programming

Chapter 2: Data and Expressions

Computational Expression

Strings and Arrays. Hendrik Speleers

AP Computer Science Unit 1. Programs

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

Zheng-Liang Lu Java Programming 45 / 79

Lecture Set 2: Starting Java

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Chapter 2 ELEMENTARY PROGRAMMING

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2: Programming Concepts

Lecture Set 2: Starting Java

Formatted Output Pearson Education, Inc. All rights reserved.

First Java Program - Output to the Screen

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Strings, characters and character literals

Welcome to the Using Objects lab!

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Program Fundamentals

More on variables and methods

Full file at

What did we talk about last time? Examples switch statements

Java Identifiers, Data Types & Variables

COMP6700/2140 Data and Types

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Full file at

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

Chapter 2 Primitive Data Types and Operations. Objectives

Review Ch 5 static Multiple Choice Test Creating Class Methods

Computer Programming, I. Laboratory Manual. Final Exam Solution

3 The Building Blocks: Data Types, Literals, and Variables

Lecture 3: Variables and assignment

JAVA Programming Fundamentals

Lecture 2 Tao Wang 1

Chapter 2: Using Data

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamentals of Programming Session 4

Building Java Programs

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Programming in C++ 4. The lexical basis of C++

Programming for Engineers Introduction to C

PROGRAMMING FUNDAMENTALS

For the course, we will be using JCreator as the IDE (Integrated Development Environment).

Building Java Programs

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Classes and Objects Part 1

Introduction to Computer Science I

Transcription:

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

Java - Character Class Normally, when we work with characters, we use primitive data types char. char ch = 'a'; // Unicode for uppercase Greek omega character char unichar = \u039a'; // an array of chars char[] chararray ={ 'a', 'b', 'c', 'd', 'e' ; However in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char. The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor: Character ch = new Character('a');

Escape Sequences A character preceded by a backslash (\) is an escape sequence and has a special meaning to the compiler. The newline character (\n) has been used frequently in this tutorial in System.out.println() statements to advance to the next line after the string is printed. Escape Sequence \t Inserts a tab in the text at this point. Description \b Inserts a backspace in the text at this point. \n Inserts a newline in the text at this point. \r Inserts a carriage return in the text at this point. \f Inserts a form feed in the text at this point. \' Inserts a single quote character in the text at this point. \" Inserts a double quote character in the text at this point. \\ Inserts a backslash character in the text at this point.

Example If you want to put quotes within quotes, you must use the escape sequence, \ ", on the interior quotes: public class Test { public static void main(string args[]) { System.out.println("She said \"Hello!\" to me."); Output: She said "Hello!" to me.

Strings Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The most direct way to create a string is to write: String greeting = "Hello world!"; As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters. public class StringDemo { public static void main(string args[]) { char[] helloarray = {'h','e','l','l','o','.'; String hellostring = new String(helloArray); System.out.println(helloString); // output : hello.

String Length Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. The following program is an example of length(), method String class. Example: public class StringDemo { public static void main(string args[]) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); System.out.println( "String Length is : " + len ); Output - 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( Ilyas"); Strings are more commonly concatenated with the + operator, as in : "Hello," + " world" + "!"

Creating Format Strings You have printf() and format() methods to print output with formatted numbers. The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object. System.out.printf("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatvar, intvar, stringvar); You can write String fs; fs = String.format("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatvar, intvar, stringvar); System.out.println(fs);

Thank you for attention