CORE JAVA: String Manipulations In Java

String

Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.The Java platform provides the String class to create and manipulate strings.

Creating Strings

The most direct way to create a string is to write −

String greeting = "Hello world!";

EXAMPLE:
String firstName;
firstName= new String ("Yogesh");
String firstName= new String(Yogesh");
String firstName= "Yogesh";
String lastName= "Kumar";
String fullName= firstName + lastName;
String city = "New" + "York";

A simple program:

class Rectangle {
int len, bdt;
Rectangle (int len, int bdt) {
this.len = len;
this.bdt = bdt;
}
double area () {
return len* bdt;
}
double perimeter () {
return 2 * (len + bdt);
}
public String toString()
{
String s = “A Rectangle Object: length = ” + len + ” Breadth = ” + bdt;
return s;
}
}
class StringDemo
{
public static void main (String[] args)
{
Rectangle r = new Rectangle (10, 6);
System.out.println(r);
System.out.println(“Area: ” + r.area());
System.out.println(“Perimeter: ” + r.perimeter());
}
}

OUTPUT:

A Rectangle Object: length = 10 Breadth = 6

Area: 60.0

Perimeter: 32.0

Conversion: Char Array and String

char[] arr={‘h’,’e’, ‘l’, ‘l’, ‘o’};
String word = new String(arr);
                     (or)
String word = String.valueOf(arr);
String word = ” bye”;
char[] arr= word.toCharArray();

Conversion: Number and String

int num=519;
String numStr= String.valueOf(num);
(or)
String numStr= Integer.toString(num);

STRING MANIPULATION METHODS:

 

1234567


 


StringBuffer Class

A string buffer is like a String, but can be modified StringBuffer class creates strings of flexible length that can be modified in terms of both length and content.

EXAMPLE 1:

class StringManipulation{

public static void main (String[] args) {

StringBuffer str= new  StringBuffer(“Object Language”);

System.out.println(“Original String ” + str);

System.out.println(“Length of String: ” + str.length());

intpos = str.indexOf(” Language”);

str= str.insert(pos, ” Oriented”);

System.out.println(str);

str.setCharAt(6,’-‘);

System.out.println(str);

str.append(” improves security.”);

System.out.println(str);

str.setLength(10);

System.out.println(str);

}

}

OUTPUT:

5.png

EXAMPLE 2:

class StringConvert

{

public static void main (String[] args)

{

System.out.println(“Enter a string: “);

String s = System.console().readLine();

StringBuffer ss= new StringBuffer(s);

System.out.println(“String in reverse: ” + ss.reverse());

}

}

OUTPUT:

Enter a string: My name is khan

String in reverse: nahk si eman ym

Conversion: StringBufferand String

String s = “Java Programming”;

StringBuffer sb= new String (s);

String s1 = sb.toString();


String Tokenizer

StringTokenizer st= new  StringTokenizer(“this is a test”);

while (st.hasMoreTokens())

{

System.out.println(st.nextToken());

}

Instead we can use split() method of String class:

class SplitDemo

{

public static void main (String[] args)

{

String s = “this is a test”;

String[] tokens = s.split(” “);

for (inti= 0; i< tokens.length;i++)

System.out.println(tokens[i]);

}

}

 

Advertisements

One thought on “CORE JAVA: String Manipulations In Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s