Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 27 April 2017

String Programs in Java

 String Programming

Here we will discuss some important string programs in java which is very important and good for interview. Let's start 


String Programs in Java



1) String Length

This is very simple program in String concept. In this program we will display the total number of characters in string.

For example :

class StringLenghtExample
{
public static void main(String args[])
{
String s1 = "java";
String s2 = "java programming";
System.out.println(s1.length());//print 4 length
System.out.println(s2.length());//print 16 space will count after java
}


output : 4
             16


2) String Concatenation in Java

By using the string concatenation we can combine the two string. In java String, there are two ways to concat string in java. these are
  • By concat() method
  • By +(plus) operator
In the below example we will concat string through + operator only.

String Concatenation by + operator

This operator(+) is used to append one string into another string.

For example 1 :

class StringConcatenationExample
{
public static void main(String args[])
{
String s1 = "java";
String s2 = "programming";
String s3 = "java"+" programming";

System.out.println(s1);
System.out.println(s2);
System.out.println(s3);//String concatenation through + operator 
}
}


output : java
              programming
              java programming

For example 2 :

class StringConcatenationExample1
{
public static void main(String args[])
{
String s = 30+70+"india"+20+30;
System.out.println(s);
}
}

output : 100india2030

In the above example 2, In java string concatenation operator(+) can concat not only string but primitive type values also.

" All the + operator will be treated as String concatenation operator after a String literal ". 


3) Java String Array

In this example we will learn how to create and String array in java.

For example :

class StringArrayExample
{
public static void main(String args[])
{

//declaration,instantiation,initialization of String array in single line

String[] fruits = {"mango","banana","orange"};
System.out.println(fruits[1]);//print first index of array i.e banana

// or

//Another way of creating String array using multiple lines

String[] clothes = new String[3];//declaration,instantiation of String
clothes[0] = "paint";//initialization of String array
clothes[1] = "shirt";
clothes[2] = "cap";

//Iterate the String array using loop
for(int i = 0; i<clothes.length; i++)
{
System.out.println(clothes[i]);
}
}
}

output : banana
              paint
              shirt
              cap



4) Java Reverse String

In this program we will not using reverse method of String. We will reverse the string without using String API.

For example :

class ReverseString
{
public static void main(String args[])
{
String str = "Hello Java";

String reverse = "";
for(int i= str.length()-1; i>=0; --i)
{
reverse+=str.charAt(i);
}
System.out.println(reverse);
}
}

output : avaJ olleH


5) String Palindrome in Java

In this program, we will check given string is palindrome or not. Palindrome means remains the same after reverse e.g Madam is palindrome because if we reverse this string it will look same madam and sir is not palindrome.

For example :

class PalindromeString
{
public static void main(String args[])
{
String str = "MADAM";
String reverse = "";

for(int i= str.length()-1; i>=0; --i)

{

reverse+=str.charAt(i);

}

System.out.println(reverse);


if(reverse.equalsIgnoreCase(str))
{
System.out.println("String is palindrome");
}
else
{
System.out.println("String is not palindrome");
}
}
}


output : MADAM


6) String Sort

In this program, We will sort the String with String API.

For example 1 :

import java.util.*;
class SortStringExample
{
public static void main(String args[])
{
String sort = "ihgfedcba";
char[] c = sort.toCharArray();
Arrays.sort(c);
String s = new String(c);
System.out.println(s);
}
}

output : abcdefghi

For example 2 :

In this program, We will sort the String without String API.

class SortStringExample2
{
public static void main(String args[])
{
String s1 = "edcba";
char temp = 0;
char[] c = s1.toCharArray();
for(int i = 0; i<c.length; i++)
{
for(int j = 0; j<c.length; j++)
{
if(c[j]>c[i])
{
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}

for(int k = 0; k<c.length; k++)
{
System.out.print(c[k]);
}
}
}

output : abcde

Share:

3 comments:

  1. Well explained string operation .
    there are good String program collection visit Top String program

    ReplyDelete
  2. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate