Skip to main content

String in Java

 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

------------------------------------------------------------------------------------------------------------------------------

Comments

Post a Comment

Popular posts from this blog

Decision Making Statements in Java Programming Language.

 Their are 3 decision making statement in Java. Here where ever you find the if underlined like this( if ) then it is related to statement. 1) if statement. 2) if else statement. 3) if-else-if statement. So lets discuss one by one about each statement. 1) if statement:- if statement is used in programming for checking the given condition is true or not. If the condition in the if block is true then the block executes otherwise the programme terminates. Lets understand it by its syntax.   Here in the syntax we have condition in the if block, if the the condition which we have putted is true then the code which is written in the block will executes otherwise the programme ends their.  2) if else statement:- if else statement is used in programming for checking the given condition is true or false. If the condition is true then the if block is executed otherwise the else block is executed. Lets understand it by its syntax. Here in the syntax a condition is given in the if...

What is Procedural Programming Language(PPL)

  Procedural Programming Language. Procedural Programming might be the primary programming worldview that another engineer will learn. Essentially, the procedural code is the one that straightforwardly trains a gadget on the best way to complete an errand in consistent advances. This worldview utilizes a direct top-down methodology and treats information and techniques as two distinct elements. In view of the idea of a methodology call, Procedural Programming isolates the program into strategies, which are otherwise called schedules or capacities, just containing a progression of steps to be done.  Basically, Procedural Programming includes recording a rundown of directions to mention to the PC what it ought to do bit by bit to complete the job needing to be done.  Key Highlights of Procedural Programming  The vital highlights of procedural writing computer programs are given underneath:  1)Predefined capacities: A predefined work is normally a guidance distingu...

Packages In Java

  Package in java is a group of similar type of classes, interface and sub-package. Packages in java can be defined in two ways which are 1 Built-in Packages 2 User-defined Packages Built-in Packages are       * Lang      * Util      * io      * awt      * net      * applet, etc. User-defined packages can be anything which are created by the user. Packages are nothing but container of the classes which store the no of classes, interface and their methods. Advantages of Packages 1 It provide access Protection 2 It removes the naming collision 3 it is used to categorize the classes and interface so that they can be easily maintained. Steps for creating a package Step-1 Create a folder in any drive of your choice  Step-2 example in d: package1 (it is the name of the package) Step-3 Open the java application and type the following code package package1; public class demo {  publ...