0% found this document useful (0 votes)
25 views

Java String Programs

Uploaded by

jyotsnas99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java String Programs

Uploaded by

jyotsnas99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java String Programs


// string buffer class demo
public class StringBuffDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1= "Hello";
StringBuffer sb= new StringBuffer("Hello");
s1.concat("Java");
sb.append("Java");
System.out.println("String s1:"+s1);
System.out.println("String sb:"+sb);

}
}

// program to find whether the given string is palindrome or not


import java.util.Scanner;
public class StringPalindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
str = sc.nextLine();
int length = str.length();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);
if (str.equals(rev))
System.out.println(str+"is a palindrome");
else
System.out.println(str+"is not a
palindrome");
}
}
// program to demonstrate string methods
public class StringDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1=" Welcome JAVA ";
String s2= new String("Hello World ");
String s3= s1.concat(s2);
String s4= "Hello world ";
System.out.println(s1.equals(s2));
System.out.println(s1.length());
System.out.println(s2.charAt(6));
System.out.println(s3.contains(s2));
System.out.println(s3.contains("Hello"));
System.out.println(s2.compareToIgnoreCase(s4));
System.out.println(s3.substring(6));
System.out.println(s3.substring(8, 12));
//System.out.println(s4.split(s1));
System.out.println(s3.replace("World",
"Globe"));
System.out.println(s4.replaceAll(s2, s1));
System.out.println(s1.startsWith(" "));
System.out.println(s4.toCharArray());
}
}

// program to count the occurrences of a character


public class Charcount {
public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "aaaabbccAAdd";
char search = 'a'; // Character to
search is 'a'.
int count=0;
for(int i=0; i<input.length(); i++)
{
if(input.charAt(i) == search)
count++;
}

System.out.println("The Character
'"+search+"' appears "+count+" times.");
}
}

// program to compare the strings


public class CompareString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "This is Exercise 1";
String str2 = "This is Exercise 2";
String str3 = "this is exercise 2";
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
// Compare the two strings.
int result = str1.compareTo(str2);
int result1 = str3.compareToIgnoreCase(str2);
// Display the results of the comparison.
if (result < 0)
{
System.out.println("\"" + str1 + "\"" +
" is less than " +
"\"" + str2 + "\"");
}
else if (result == 0)
{
System.out.println("\"" + str1 + "\"" +
" is equal to " +
"\"" + str2 + "\"");
}
else // if (result > 0)
{
System.out.println("\"" + str1 + "\"" +
" is greater than " +
"\"" + str2 + "\"");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy