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

Best Programming Language to learn in 2021

The main aptitude to learn in this day and age is to realize how to compose a PC program. Today, PCs have entered in pretty much every industry. Be it the autopilot in an airplane or advanced speedometer in your bicycle, PCs in different structures encompass us. PCs are incredibly valuable for an association to scale up well. Gone are the times of pen and paper. Today, to store and access your data, you totally need PCs. The programming and engineer networks are arising at a rate quicker than at any other time. Different new programming dialects are coming up that are appropriate for various classifications of designers (tenderfoots, middle, and specialists) just as for various use cases (web application, versatile applications, game turn of events, disseminated framework, and so forth) Each amateur is astounded with the inquiry, "What programming language would it be advisable for me to learn?" Types of Programming  1. Procedural Programming Languages This programming worldv

Java - Static variable, static method and static block with example.

Java Static keyword, Static method and Static block........ Questions.......?? 1) What is static keyword , static method() , static block 2) Define static keyword , static method() , static block 3) What do you mean by static keyword , static method() , static block 4)static keyword , static method() , static block in Java ?     1)Static Keyword:- Static keyword in Java Programming language is used for memory management purpose, it indicates that a particular member belong to a type itself, rather than to an instance of that type. As we mentioned above that is used for memory management purpose it means that static keyword gets memory once in the class which help in reduction of the memory, which makes the execution speed of the program fast which is a great advantage of the static keyword. let's take example how static keyword works..... firstly we will see how memory get wasted when we didn't use the static keyword. class company {  int emp_id; 

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 distinguished by a name. For the