1 / 7

Strings

Strings. The String is not a siple data type, it is a class. The String class represents character strings. All string literals in Java programs, such as " abc ", are implemented as instances of this class.

hallie
Download Presentation

Strings

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. Strings • The String is not a siple data type, it is a class. • The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. • Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. • For example: • String str = "abc"; • is equivalent to: • char[] data = {'a', 'b', 'c'}; • String str = new String(data); • The String class is final. The String class contains • several methods that allow us to accomplish a variety • of string manipulation tasks. • Eg:test.java • Palindrome.java

  2. String is a predefined Java class (in java.lang.String) • a String object encapsulates a sequence of characters • String firstName = "Dave"; • which is equivalent to • String firstName = new String("Dave"); • you can display Strings using System.out.print and System.out.println • System.out.println(firstName); • the '+' operator concatenates two strings (or string and number) together • String str = "foo" + "lish"; • str = str + "ly"; • int age = 19; • System.out.println("Next year, you will be " + (age+1));

  3. Difference between Character Array and String Eg:chararraytest.java Chararray.cpp

  4. String Methods Method Call Task Performed s2 = s1.toLowerCase() Converts the string S1 to all lowercase s2 = s1.toUpperCase() Converts the string S1 to all uppercase s2 = s1.replace(‘o’,’i’); Replace all appearances of o with i s2 = s1.trim(x); Remove white spaces at the beginning and end of the string S1 S1.equals(S2) Returns ‘true’ if S1 is equals to S2 S1.equalsIgnoreCase(S2) Returns ‘true’ if S1 = S2, ignoring the case of characters S1.length() Gives the length of S1 S1.chartAt (n) Gives nth character of S1 S1.compareTo(S2) Returns negative if S1<S2, positive if S1>S2, and zero if S1 = S2 S1.concat(S2) Concatenates S1 and S2 S1.substring(n) Gives substring starting from nth character S1.substring(n,m) Gives substring starting from nth character up to mth (not including mth) String.valueOf(p) Creates a string object of the parameter p Eg:stringtest.java

  5. StringBuffer • It is a peer class of string. While string creates strings of fixed length, string buffer creates strings of flexible length that can be modified in terms of both length and content. • We can insert characters and sub strings in the middle of the string, or append another string to the end.

  6. Constructors for StringBuffer 1.StringBuffer() – 16 characters 2.StringBuffer(int size) 3.StringBuffer(String str) Every StringBuffer has two methods 1.length() – no. of characters 2.capacity() - no. of characters, it can contain without being expanded.

  7. Commonly Used StringBuffer Methods: S1.setCharAt(n,’x’) : Modifies the nth character to x S1.append(S2) : Appends the strings S2 to S1 at the end S1.insert(n,S2) : Inserts the string S2 at the position n of the string S1 S1.reverse(String s) Eg:stringbuf.java

More Related