Topics to be covered...
1) String in Java
2) string literal,new keyword in java
3) string array in java
4) What is mutable string ?
5) Java StringBuffer class | StringBuffered methods()
6) reverse a string in java | how to reverse a string in java | string reverse in Java
7) string program in java
---------------------------------------------------------------------------------------------------------------------------
*1)String:- string is generally a sequence of characters, but in java programming language string it is a sequence is an object which represents the sequence of the characters.
--------------------------------------------------------------------------------------------------------------------------------
*2)string literal and new keyword:- string literal in java is created by using double quotes " ", let's assume an example
String s=" Welcom to Programming Worlds and Facts..";
now let's see how to allocate string new keyword
String s1=" programming";
String s2=new String("Programming"); // by allocating new keyword
--------------------------------------------------------------------------------------------------------------------------------
*3) string array in java:- As we know that array is a collection of similar data type and it is stored in continuous memory location, and we already discussed that string is a sequence of characters.It is considered as the immutable object that is it cannot be get changed, the purpose of string array is same as of it. It is used to store a fixed numner of Strings.
Declaration of string array in java is:
String[] obj; // declared without new keyword and without fixed size.
String[] obj=new String[2]; // declared by allocating new memory and declared with fixed size.
Both these methods are used for the declaration of the string array in java.
..)Initializing A String Array In Java....
class stringarray
{
public static void main(String[] args)
{
String[] obj={"J","A","V","A"};
String[] obj1=new String[3];
obj1[0]="A";
obj1[1]="B";
obj1[2]="C";
}
}
-----------------------------------------------------------------------------------------------------------------------------
*4) Mutable String in Java:- Mutable String is a type of String which can be changed or modified. StringBuffer and StringBuilder are the class which are used for creation of mutable string.....
--------------------------------------------------------------------------------------------------------------------------------
*5) Java StringBuffer class:- StringBuffer class in Java is used to create mutable String. The StringBuffer class in java is same as of String class, the only exception is mutable which means it can be changed.
Let's discuss some constructors of StringBuffer class
1) StringBuffer() :- It is used to create an empty String buffer with its initial capacity of sixteen(16).
2) StringBuffer(String obj) :- It is used to create a string buffer with the specified string.
3)StringBuffer(int capacity) :- It is used to create an empty string buffer with the specified capacity as length.
Let's check out some methods which are available for StringBuffer..
1) StringBuffered insert() method...
This method is used to insert the given string at any position where the user wants to, lets see an example
class Stringbufferedexample
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcomjava");
obj.insert (6,"eto");
System.out.println(obj);
}
}
Output
Welcometojava
2) StringBuffer append() method.....
This method is used to concatinates the given argument with the string,let's see an example
class Stringbufferedexample1
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome ");
obj.append(" Programmers");
System.out.println(obj);
}
}
Output
Welcome Programmers
1) StringBuffered insert() method...
This method is used to insert the given string at any position where the user wants to, lets see an example
class Stringbufferedexample
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcomjava");
obj.insert (6,"eto");
System.out.println(obj);
}
}
Output
Welcometojava
2) StringBuffer append() method.....
This method is used to concatinates the given argument with the string,let's see an example
class Stringbufferedexample1
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome ");
obj.append(" Programmers");
System.out.println(obj);
}
}
Output
Welcome Programmers
1) StringBuffered insert() method...
This method is used to insert the given string at any position where the user wants to, lets see an example
class Stringbufferedexample
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcomjava");
obj.insert (6,"eto");
System.out.println(obj);
}
}
Output
Welcometojava
2) StringBuffer append() method.....
This method is used to concatinates the given argument with the string,let's see an example
class Stringbufferedexample1
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome ");
obj.append(" Programmers");
System.out.println(obj);
}
}
Output
Welcome Programmers
3) StringBuffered replace() method...
This method is used to replace the given string from the specified beginIndex and the endIndex, let's see an example
class Stringbufferedexample2
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome");
obj.replace (1,3,"java");
System.out.println(obj);
}
}
Output
Wjavacome
4) StringBuffer delete() method.....
This method is used to delete the string from the specified beginIndex to endIndex,let's see an example
class Stringbufferedexample3
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome ");
obj.delete(1,3);
System.out.println(obj);
}
}
Output
Wlome
5) StringBuffer reverse() method.....
This method is used to reverse the current string,let's see an example
class Stringbufferedexample4
{
public static void main(String[] args)
{
StringBuffer obj= new StringBuffer("Welcome ");
obj.reverse();
System.out.println(obj);
}
}
Output
emocleW
-----------------------------------------------------------------------------------------------------------------------------
*6)reverse a string in java | how to reverse a string in java | string reverse in Java :-
In the above point we have seen how to reverse the given string, another method to reverse a string is given below:-
import java.io.*;
class reveresing_string_in_java
{
public static void main(String[] args)
{
BufferedReader r= new BufferedReader(new InputStreamreader(System.in));
System.out.println("Enter the String");
String s= r.readLine();
System.out.print("Reverse order of the entered string is:");
for(int i=0;i<s.lenght();i++)
{
System.out.println(s.charAt(i));
}
System.out.println();
}
}
Output
Enter the String
Programming_Worlds_and_Facts..
Reverse order of the entered string is: ..stcaF_dna_sdlroW_gnimmargorP
------------------------------------------------------------------------------------------------------------------------------
Really niceππ
ReplyDeleteThanks buddy ☺️π
Delete