670 likes | 801 Views
F4105 JAVA PROGRAMMING. F4105 Java Programming. 3 .0 CLASS, POLYMORPHISM AND INHERITANCE. 3.4 String. F4105 JAVA PROGRAMMING. LEARNING OUTCOMES TOPIC 3.4. By the end of this chapter students shall be able to :. F4105 JAVAPROGRAMMING. Introduction.
E N D
F4105 JAVA PROGRAMMING F4105 Java Programming
3.0 CLASS, POLYMORPHISM AND INHERITANCE 3.4 String
F4105 JAVA PROGRAMMING LEARNING OUTCOMES TOPIC 3.4 By the end of this chapter students shall be able to :
F4105 JAVAPROGRAMMING Introduction • A string is a sequence of characters.
F4105 JAVAPROGRAMMING String Declaration Example 1 char str [ ] = {'a','e','i','o','u'};
F4105 JAVAPROGRAMMING String Declaration Example 2 char text[ ] = {'I','','C','A','N','','W','R','I','T', 'E','A','','G','O','O','D','','P','R','O','G','R','A','M','I','N','','J','A','V','A'}; Example 3 String text = "I CAN WRITE A GOOD PROGRAM IN JAVA";
F4105 JAVAPROGRAMMING java.lang Package class String class StringBuffer method method
F4105 JAVAPROGRAMMING String Class • String class is present in java.lang package. • The methods in this class are used to manipulate strings. • The string objects created in a String class cannot be changed. Example: String obj = new String("Welcome"); obj.append("_World"); System.out.println(obj); wrong
F4105 JAVAPROGRAMMING Creating String Object Example 1: Syntax String stringobject = new String( ); Example: String s=new String(“Java”);
F4105 JAVAPROGRAMMING Creating String Object Example 2 Syntax String stringobject = <string value>; Example: String s = "I have to study for my exams";
F4105 JAVAPROGRAMMING Creating String Object Example 3 Syntax String stringobject = new String(array_name); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr);
F4105 JAVAPROGRAMMING Creating String Object Example 4 Syntax String stringobject = new String(stringobject); Example: char arr[ ] = {'H','E','L','L','O'}; String s = new String(arr); String s1 = new String(s);
F4105 JAVAPROGRAMMING String Methods • The String class has several methods that can be used to manipulate the string values. • Each method has its own characteristic.
F4105 JAVAPROGRAMMING length() method • Is used to find the number of characters in a string.
F4105 JAVAPROGRAMMING length() method Example 1 String s = "I am going to school"; intnumchar = s.length( ); System.out.println(numchar); Output 20
F4105 JAVAPROGRAMMING length() method Example 2 String s = " "; intnumchar = s.length( ); System.out.println(numchar); Output 2
F4105 JAVAPROGRAMMING length() method Syntax intvariable_name = stringobject.length( );
F4105 JAVAPROGRAMMING concat() method Example 1 String firststring = "The Twin Tower"; String secondstring = " looks beautiful"; String thirdstring = firststring.concat(secondstring); @ firststring+secondstring Output The Twin Tower looks beautiful
F4105 JAVAPROGRAMMING concat() method Syntax String Stringvariable3 = stringvariable1.concat (stringvariable2);
F4105 JAVAPROGRAMMING concat() method Example 2 String s1 = "Result: "+5+1; System.out.println(s1); Output Result: 51
F4105 JAVAPROGRAMMING concat() method Example3 String s2 = "Result: "+(5+1); System.out.println(s2); Output Result: 6
F4105 JAVAPROGRAMMING charAt() method • Extracts a single character from a string
F4105 JAVAPROGRAMMING charAt() method Example String s = "Malaysia" char ch = s.charAt(4); System.out.println(ch); Output y
F4105 JAVAPROGRAMMING charAt() method Syntax char variablename = stringobject.charAt(int where);
F4105 JAVAPROGRAMMING equals() method • Compares two strings for equality.
F4105 JAVAPROGRAMMING equals() method Example String s1="Both are equal"; String s2="Both are equal"; String s3="Both are not equal"; System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); Output true false
F4105 JAVAPROGRAMMING equals() method Syntax booleanvariablename = stringobject.equals(stringobject);
F4105 JAVAPROGRAMMING indexOf() method • Will search the first occurrence of a character or set of characters inside a string.
indexOf() method F4105 JAVAPROGRAMMING Example 1 int val1 = s.indexOf('s'); System.out.println(val1); Output 3
F4105 JAVAPROGRAMMING indexOf() method Example 2 int val2 = s.indexOf("is"); System.out.println(val2); Output 2
F4105 JAVAPROGRAMMING indexOf() method Syntax int variablename = stringobject.indexOf(character); int variablename = stringobject.indexOf(string);
F4105 JAVAPROGRAMMING lastIndexOf() method • Will search the last occurrence of a character or a set of characters inside a string.
lastIndexOf() method F4105 JAVAPROGRAMMING Example 1 int val1 = s.lastIndexOf('s'); System.out.println(val1); Output 12
F4105 JAVAPROGRAMMING lastIndexOf() method Example 2 int val2 = s.lastIndexOf("is"); System.out.println(val1); Output 5
F4105 JAVAPROGRAMMING lastIndexOf() method Syntax int variablename = stringobject.lastIndexOf(character); int variablename = stringobject.lastIndexOf(string);
F4105 JAVAPROGRAMMING replace() method • Will replace all occurrences of a character in a string with another character.
F4105 JAVAPROGRAMMING replace() method Example String s="Hewwo"; String s1 = s.replace('w','l'); System.out.println(“Old string= “ + s); System.out.println(“New string= “ + s1); Output Hewwo Hello
F4105 JAVAPROGRAMMING replace() method Syntax String stringvariable = stringobject.replace(original,replacement);
F4105 JAVAPROGRAMMING trim() method • Will trim the leading and trailing white spaces in a string.
F4105 JAVAPROGRAMMING trim() method Example String s=" Hello Malaysia "; String s1 = s.trim( ); System.out.println(s1); Output Hello Malaysia
F4105 JAVAPROGRAMMING trim() method Syntax String stringvariable = stringobject.trim( );
F4105 JAVAPROGRAMMING toLowerCase() method • Converts all the characters in a string from uppercase to lowercase.
F4105 JAVAPROGRAMMING toLowerCase() method Example String s1="LOWER"; String s2 = s1.toLowerCase( ); System.out.println(s2); Output lower
F4105 JAVAPROGRAMMING toLowerCase() method Syntax String stringvariable = stringobject.toLowerCase( );
F4105 JAVAPROGRAMMING toUpperCase() method • Converts all the characters in a string from lowercase to uppercase.
F4105 JAVAPROGRAMMING toUpperCase() method Example String s1="upper"; String s2 = s1.toUpperCase( ); System.out.println(s2); Output UPPER
F4105 JAVAPROGRAMMING toUpperCase() method Syntax String stringvariable = stringobject.toUpperCase( );
F4105 JAVAPROGRAMMING substring() method • Extracts a part of the string.
F4105 JAVAPROGRAMMING substring() method Example String s1="I do shopping in Mega Mall"; String s2 = s1.substring(5,12); System.out.println(s2); Akrasake (5) sehinggaaksarake(12-1) Output shoppin
StringBufferClass • Is a special class supported by Java to handle strings. • StringBuffercreates strings of flexible length whereas Stringclass creates strings of fixed length.