Skip to main content

String and its methods in java

Java String Class and its methods

Java String provides a lot of concepts that can be performed on a string such as compare, concat , equals, split, length, replace, compareTo, intern, substring etc.
In java, string is basically an object that represents sequence of char values.

Creating a String

There are two ways to create a String in Java
  1. String literal
  2. Using new keyword

String literal

In java, Strings can be created like this: Assigning a String literal to a String instance:
String string = "Welcome";
String string1 = "Here";
The problem with this approach: As I stated in the beginning that String is an object in Java. However we have not created any string object using new keyword above. The compiler does that task for us it creates a string object having the string literal (that we have provided , in this case it is “Welcome”) and assigns it to the provided string instances.
But if the object already exist in the memory it does not create a new Object rather it assigns the same old object to the new instance, that means even though we have two string instances above(string and string1) compiler only created on string object (having the value “Welcome”) and assigned the same to both the instances. For example there are 10 string instances that have same value, it means that in memory there is only one object having the value and all the 10 string instances would be pointing to the same object.
What if we want to have two different object with the same string? For that we would need to create strings using new keyword.

Using New Keyword

As we saw above that when we tried to assign the same string object to two different literals, compiler only created one object and made both of the literals to point the same object. To overcome that approach we can create strings like this:
String string = new String("Hi");
String string1 = new String("Hi");
In this case compiler would create two different object in memory having the same text.

String Methods

  1. char charAt(int index): 
  2. int codePointAt(int index):
  3. void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin):
  4. boolean equals(Object obj): 
  5. boolean contentEquals(StringBuffer sb):
  6. boolean equalsIgnoreCase(String string):
  7. int compareTo(String string):
  8. int compareToIgnoreCase(String string):
  9. boolean regionMatches(int srcoffset, String dest, int destoffset, int len):
  10. boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len):
  11. boolean startsWith(String prefix, int offset):
  12. boolean startsWith(String prefix): 
  13. boolean endsWith(String suffix): 
  14. int hashCode():
  15. int indexOf(int ch):
  16. int indexOf(int ch, int fromIndex): 
  17. int lastIndexOf(int ch): 
  18. int lastIndexOf(int ch, int fromIndex): 
  19. int indexOf(String str): 
  20. int lastindexOf(String str): 
  21. String substring(int beginIndex): 
  22. String substring(int beginIndex, int endIndex)
  23. String concat(String str)
  24. String replace(char oldChar, char newChar)
  25. boolean contains(CharSequence s)
  26. String replaceFirst(String regex, String replacement)
  27. String replaceAll(String regex, String replacement)
  28. String[] split(String regex, int limit)
  29. String[] split(String regex)
  30. String toLowerCase(Locale locale)
  31. String toLowerCase()
  32. String toUpperCase(Locale locale)
  33. String toUpperCase()
  34. String trim()
  35. char[] toCharArray()
  36. static String copyValueOf(char[] data)
  37. static String copyValueOf(char[] data, int offset, int count)
  38. static String valueOf(data type)
  39. byte[] getBytes(String charsetName)
  40. byte[] getBytes()
  41. int length()
  42. boolean ()

Comments

Popular posts from this blog

Switch Case

Syntax : Switch Case in Java Programming It is alternative to else-if ladder. Switch Case Syntax is similar to – C/C++  Switch. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an  int, a String, or an enumerated value . switch (selection) { // value case value1 : // checking value 1 statement ( s ) ; break ; // use to break switch flow if condition match case value2 : // checking value 2 statement ( s ) ; break ; . . case value_n : statement ( s ) ; break ; default : statement ( s ) ; } Different Ways of Using Switch Case : Switch Case Using Integer Case int i=3; switch (i) { case 1 : System . out . println ( "One player is playing this game." ) ; break ; case 2 : System . out . println ( "Two players are playing

Inheritance in Java

Inheritance in Java Inheritance  is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class . The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class. Inheritance is a  compile-time  mechanism. A super-class can have any number of subclasses . But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have  “is-a”  relationship between them. Let’s have a look at the example below. Inheritance  Example Let’s consider a superclass  Vehicle . Different vehicles have different features and properties however there few of them are common to

Multilevel inheritance

Multilevel inheritance We discussed a bit about  Multilevel inheritance  in types of inheritance in java. In this tutorial we will explain multilevel inheritance with the help of   diagram  and  example program . It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance. Example : In this example we have three classes –  Car, Maruti and Maruti800. We have done a setup – class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car { // clas