1 / 35

String class

String class. What data types have we seen so far?. Data types we’ve seen so far…. integer types: int (also short, char, and byte) real numbers: double and float boolean. Introducing the String. S tring Case sensitive (capital S is required ) Declaring a String

stack
Download Presentation

String class

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. String class

  2. What data types have we seen so far?

  3. Data types we’ve seen so far… integer types: int (also short, char, and byte) real numbers: double and float boolean

  4. Introducing the String String Case sensitive (capital S is required) Declaring a String How did we declare variables of the other types (such as int, double, boolean) in the past?

  5. Declarations How have we already declared variables? int i; float f; double x; boolean isAChicken; So how do we declare Strings?

  6. Declaring Strings Declare String s; Declare and initialize (assign) String name = “John”;

  7. The Class String • String is NOT a primitive type! • The class String is a predefined class in Java that is used to store and process strings • Objects of type String are made up of strings of characters that are written within double quotes • Any quoted string is a constant of type String "Live long and prosper." • A variable of type String can be given the value of a String object String blessing = "Live long and prosper.";

  8. Concatenation of Strings • Concatenation: Using the + operator on two strings in order to connect them to form one longer string • If greeting is equal to "Hello", and javaClass is equal to "class", then greeting + javaClass is equal to "Hello class" • Any number of strings can be concatenated together • When a string is combined with almost any other type of item, the result is a string • "The answer is " + 42 evaluates to "The answer is 42"

  9. Output • int answer = 2 + 2; System.out.println(answer); //when argument to println int variable answer is //automatically converted to a string and output as “4” • System.out.println(“2 plus 2 is “ + answer); //“+” operator here “concatenates” (connects) two strings.

  10. Output • int x = 2; int y = 2; System.out.println(x + y); //expresssion is evaluated and “4” is //printed // “+” operator here is the addition operator!!!

  11. Classes, Objects, and Methods • A class is the name for a type whose values are objects • Objects are entities that store data and take actions • Objects of the String class store data consisting of strings of characters • The actions that an object can take are called methods • Methods perform an action • Methods can return (produce) a value of a single type

  12. Classes, Objects, and Methods • Recall: Invoking or calling a method: a method is called into action by writing the name of the calling object, followed by a dot, followed by the method name, followed by parentheses • This is sometimes referred to as sending a message to the object • The parentheses contain the information (if any) needed by the method • This information is called an argument (or arguments)

  13. String Methods • The String class contains many useful methods for string-processing applications • A String method is called by writing a String object, a dot, the name of the method, and a pair of parentheses to enclose any arguments

  14. String methods Useful String method: testing for equality String s = “hello”; s.equals(“help”); s.equals(“Hello”); What type of value does equals(..) return??

  15. String methods Useful String method: testing for equality String s = “hello”; s.equals(“help”); //returns false s.equals(“Hello”); //returns false

  16. String methods The value returned by a method may be stored in a variable of correct type. String s = “hello”; boolean returnValue = s.equals(“help”); //returns false

  17. String methods equals() is case sensitive (i.e., it distinguishes between upper and lower case) “hello” is not the same as “Hello” (Don’t use == to compare two string objects)

  18. String methods Wouldn’t it be nice to have a method that is like equals() but ignores case? “hello” is the same as “Hello” What would be a good name for such a method?

  19. String s = “HeLlo”; s.equalsIgnoreCase(“hello”); //returns true

  20. More String methods length() startsWith() endsWith() toLowerCase() toUpperCase() charAt() substring()

  21. String method: length() What (type of thing) does length() return?

  22. String method: length() What (type of thing) does length() return? An integer int length() Return type is int No arguments

  23. String Methods • If a String method returns a value, then it can be placed anywhere that a value of its type can be used String greeting = "Hello"; int count = greeting.length(); //count’s value is 5

  24. String Methods String greeting = "Hello"; System.out.println("Length is " + greeting.length());

  25. String Indexes

  26. More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String name = “John”; char c = name.charAt(2); //returns ‘h’

  27. More string methods substring() (Two of them.) String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex to the end of the string). String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1).

  28. substring(..) • String example = “this is a long string.”; String substr = example.substring(8); //returns “a long string.”

  29. substring(..) • String example = “this is a long string.”; String substr = example.substring(1, 4); //returns “his”

  30. More String methods startsWith() and endsWith() Returns true if the string starts with (ends with) the specified string argument. String s1 = “dish”; String s2 = “dishes”; boolean plural = s1.endsWith(“es”);

  31. More String functions: toLowerCase() and toUpperCase() String s1 = “dIsHeS”; String s2 = s1.toLowerCase(); //s2 is “dishes”

  32. And many more!

  33. Escape Sequences • A backslash (\) immediately preceding a character (i.e., without any space) denotes an escape sequence or an escape character • The character following the backslash does not have its usual meaning • Although it is formed using two symbols, it is regarded as a single character

  34. Escape Sequences

  35. String Processing • A String object in Java is considered to be immutable, i.e., the characters it contains cannot be changed • There is another class in Java called StringBuffer that has methods for editing its string objects • However, it is possible to change the value of a String variable by using an assignment statement String name = "Soprano"; name = "Anthony " + name;

More Related