CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.

Slides:



Advertisements
Similar presentations
Java
Advertisements

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
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.
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.
Java Strings in 10 minutes
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Fundamental Programming Structures in Java: Strings.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
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.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
String Handling StringBuffer class character class StringTokenizer class.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
String Object String is a sequence of characters. Unlike many other programming languages that implements string as character arrays, Java implements strings.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
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.
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.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
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.
Strings Can view as array of chartacters –Implemented as a class with its own methods Simplest operation on strings is catenating two strings String x.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Chapter 8 String Manipulation
The Methods and What You Need to Know for the AP Exam
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
EKT 472: Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String String Builder.
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
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
String Handling String, StringBuffer, StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
Strings in Java.
Object Oriented Programming
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Strings in Java Strings in Java are also a reference type.
Unit-2 Objects and Classes
Presentation transcript:

CSI 3125, Preliminaries, page 1 String

CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings. Creating Strings: The most direct way to create a string is String greeting = "Hello world!";

CSI 3125, Preliminaries, page 3 String Class Constructor The String class supports several constructors. To create an empty String, call the default constructor. String s = new String();

CSI 3125, Preliminaries, page 4 String Class Parameterized Constructor (1) To create a String initialized by an array of characters, use the constructor public class StringDemo{ public static void main(String args[]){ char[] helloArray = { ‘p', ‘o', ‘p', ‘o'}; String S = new String(helloArray); System.out.println(S ); } } This constructor initializes S with the string “popo”.

CSI 3125, Preliminaries, page 5 String Class Parameterized Constructor (2) Can specify a subrange of a character array as an initializer using the following constructor: String(char chars[ ], int startIndex, int numChars) startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example: char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; String s = new String(chars, 2, 3); This initializes s with the characters cde.

CSI 3125, Preliminaries, page 6 String Class Parameterized Constructor (3) Copy one String object to another Using the following constructor String(String strObj) - strObj is a String object. class MakeString { public static void main(String args[]) { char c[] = {'J', 'a', 'v', 'a'}; String s1 = new String(c); String s2 = new String(s1); System.out.println(s1); System.out.println(s2); }}

CSI 3125, Preliminaries, page 7 String Class Parameterized Constructor (4) Initializing string with ascii characters String(byte asciiChars[ ]) String(byte asciiChars[ ], int startIndex, int numChars) class SubStringCons { public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 }; String s1 = new String(ascii); System.out.println(s1); String s2 = new String(ascii, 2, 3); System.out.println(s2); }} o/p ABCDEF CDE

CSI 3125, Preliminaries, page 8 String Length The length() method, which returns the number of characters contained in the string object. char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System.out.println(s.length()); 3 String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); System.out.println( "String Length is : " + len ); 7

CSI 3125, Preliminaries, page 9 String equals compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true. eg String s1="javatpoint"; String s2="JAVATPOINT"; System.out.println(s1.equals(s2 ));//false because case is not same

CSI 3125, Preliminaries, page 10 String equalsIgnoreCase To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z String s1="javatpoint"; String s2="JAVATPOINT"; System.out.println(s1.equalsIgnoreCase(s2) );//true

CSI 3125, Preliminaries, page 11 String Compare compareTo( ) It returns integer value eg Str1.compareTo(str2); Str1 & str2 are String Object Less than zero The invoking string is less than str2. Greater than zero The invoking string is greater than str2. Zero The two strings are equal.

CSI 3125, Preliminaries, page 12 String Concatenation string1.concat(string2); This returns a new string that is string1 with string2 added to it at the end. "My name is ".concat(“POPO"); commonly concatenated with the + operator, as "Hello," + " world" + "!"

CSI 3125, Preliminaries, page 13 String Search The String class provides two methods that allow to search a string for a specified character or substring: ■ indexOf( ) Searches for the first occurrence of a character or substring. ■ lastIndexOf( ) Searches for the last occurrence of a character or substring. Eg Strint s=“ajith”; s.lastIndexOf('t')  3

CSI 3125, Preliminaries, page 14 String substring( ) extract a substring using substring( ). It has two forms String substring(int startIndex) startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string. The second form of substring( ) allows to specify both the beginning and ending index of the substring: String substring(int startIndex, int endIndex)

CSI 3125, Preliminaries, page 15 String contains searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false. eg String name="what do you know about me"; System.out.println(name.contains("do you know")); o/p true

CSI 3125, Preliminaries, page 16 String replace replace( ) The replace( ) method replaces all occurrences of one character in the invoking string with another character. String replace(char original, char replacement) String s = "Hello“; S..replace('l', 'w'); puts the string “Hewwo” into s.

CSI 3125, Preliminaries, page 17 String trim() The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. String s=“ hello “; S.trim(); “hello”

CSI 3125, Preliminaries, page 18 String format method returns the formatted string by given format and arguments. Eg String name=“popo"; String sf1=String.format("name is %s",name); String sf2=String.format("value is %f", ); String sf3=String.format("value is %32.12f", );//returns 12 char fra ctional part filling with 0 System.out.println(sf1); System.out.println(sf2); System.out.println(sf3); o/p name is spopo value is value is

CSI 3125, Preliminaries, page 19 String change case String toLowerCase( ) String toUpperCase( )

CSI 3125, Preliminaries, page 20 String