1 / 27

Chapter 3: String

Chapter 3: String. String and String Method. Chapter Objectives. String Class Commonly Used String Methods Parsing Numeric Strings. Contains operations to manipulate strings. String: Sequence of zero or more characters. Enclosed in double quotation marks.

Télécharger la présentation

Chapter 3: String

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: String String and String Method

  2. Chapter Objectives • String Class • Commonly Used String Methods • Parsing Numeric Strings

  3. Contains operations to manipulate strings. String: Sequence of zero or more characters. Enclosed in double quotation marks. Is processed as a single unit . Null or empty strings have no characters. ““ Every character has a relative position , the first character is in position 0 . The classString

  4. Java system automatically makes the class String available (i.e no need to import this class ) Example : Consider the following declaration : String sentence ; sentence = “programming with java”; The classString Java Programming: From Problem Analysis to Program Design, Second Edition 4

  5. Length of the string is the number of characters in it. When determining the length of a string, blanks count. Example : ““ has length = 0 “abc”  has length = 3, position of a = 0, b= 1, c= 2 “a boy”  has length = 5 The classString

  6. Strings and the Operator + • Operator + can be used to concatenate two strings, or a string and a numeric value or character. Example String str; intnum1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226";

  7. Strings and the Operator + • Consider the following statement: str = "The sum = " + (num1 + num2); • In this statement, because of the parentheses, you first evaluate num1 + num2. • Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38";

  8. Some Commonly Used String Methods • Suppose that String sentence = "Programming with Java";. • Then each character in sentence and its position is as follows:

  9. charAt (index): Returns the character at the position specified by index note :index should be >= 0 and <length

  10. indexOf (ch): Returns the index of the first occurrence of the character specified by ch; If the character specified by ch does not appear in the string, it returns –1

  11. indexOf (ch,pos): Returns the index of the first occurrence of the character specified by ch; The parameter pos specifies where to begin the search; If the character specified by ch does not appear in the string, it returns –1

  12. indexOf (str): Returns the index of the first occurrence of the string specified by str; If the string specified by str does not appear in the string, it returns –1

  13. indexOf (str,pos): Returns the ?? Returns the index of the first occurrence of the String specified by str; The parameter pos specifies where to begin the search; If the string specified by str does not appear in the string, it returns -1

  14. concat(str) Returns the string that is this string concatenated with str StringnStr=sentence + "is fun."; Programming with Java is fun.

  15. length() Returns the length of the string 21

  16. replace (ToBeReplaced, ReplacedWith ) Returns the string in which every occurrence of charToBeReplaced is replaced with charReplacedWith Progr*mming with J*v*

  17. substring (beginIndex,endIndex) Returns the string which is a substring of this string beginning at beginIndex until endIndex – 1 note : 1. if we did not specify the endIndex, substring of this string beginning at beginIndex until the end of the string. 2. beginIndex<endIndex amming

  18. toLowerCase() Returns the string that is the same as this string, except that all uppercase letters of this string are replaced with their equivalent lowercase letters programming with java

  19. toUpperCase() Returns the string that is the same as this string, except that all lowercase letters of this string are replaced with their equivalent uppercase letters PROGRAMMING WITH JAVA

  20. equals(str) Returns true if this string is same as str False

  21. compareTo(str) Compares two strings character by characterReturns a negative value if this string is less than str Returns 0 if this string is same as strReturns a positive value if this string is greater than str

  22. String s1 , s2 , s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG Examples on string methods Java Programming: From Problem Analysis to Program Design, Second Edition

  23. System.out.println(s1.substring(1 , 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’ ,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6 Examples on string methods Java Programming: From Problem Analysis to Program Design, Second Edition

  24. Parsing Numeric Strings • Integer, Float, and Double are classes designed to convert a numeric string into a number. • These classes are called wrapper classes. • parseInt is a method of the classInteger, which converts a numeric integer string into a value of the type int. • parseFloat is a method of the classFloat and is used to convert a numeric decimal string into an equivalent value of the type float. • parseDouble is a method of the classDouble, which is used to convert a numeric decimal string into an equivalent value of the type double. Java Programming: From Problem Analysis to Program Design, Second Edition

  25. Parsing Numeric Strings • A string consisting of only integers or decimal numbers is called a numeric string. • To convert a string consisting of an integer to a value of the type int, we use the following expression: • Integer.parseInt(strExpression) • Example: • inti=Integer.parseInt("6723"); --> 6723 • int j=Integer.parseInt("-823");--> -823 Java Programming: From Problem Analysis to Program Design, Second Edition

  26. Parsing Numeric Strings • To convert a string consisting of a decimal number to a value of the type float, we use the following expression: • Float.parseFloat(strExpression) • Example: • float f1=Float.parseFloat("34.56"); -> 34.56 • float f2=Float.parseFloat("-542.97"); -> -542.97 • To convert a string consisting of a decimal number to a value of the type double, we use the following expression: • Double.parseDouble(strExpression) • Example: • double y=Double.parseDouble("345.78");--> 345.78 • double z=Double.parseDouble("-782.873");--> -782.873 Java Programming: From Problem Analysis to Program Design, Second Edition

  27. What is the value of each variable String sentence; sentence="Now is the time for the birthday party."; int index = sentence.indexOf("birthday"); String str1 = sentence.substring(index, index + 14); intlen =sentence.length() ;

More Related