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

String Programs

The document contains Java programs demonstrating string manipulation techniques, including removing vowels, reversing strings, and using various string methods. It explains the mutability of strings in Java, highlighting the difference between string literals and string objects. Each program includes examples and outputs to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

String Programs

The document contains Java programs demonstrating string manipulation techniques, including removing vowels, reversing strings, and using various string methods. It explains the mutability of strings in Java, highlighting the difference between string literals and string objects. Each program includes examples and outputs to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) Remove Vowels from String:

package StringInJava;

public class RemoveVowelsFromString {

public static void main(String[] args) {

// TODO Auto-generated method stub

String S = "Hello, World!";

String R = S.replaceAll("(?i)[aeiou]", ""); // (?i) makes


it case-insensitive

System.out.println("Original String: " + S);

System.out.println("String after removing vowels: " + R);

// The regular expression "(?i)[aeiou]":

// (?i) makes the regex case-insensitive.

// [aeiou] matches any vowel (both uppercase and lowercase).

// The replaceAll() method replaces all occurrences of vowels with an empty


string "", effectively removing them from the string.

2) Reverse String:
package StringInJava;

public class ReverseStringProgram {

public static void main(String[] args) {

// TODO Auto-generated method stub

String s="madam";

String t=" ";

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

t=t + s.charAt(i);

System.out.println(t);

if(s.equals(t))

System.out.println("Given string is pollindrome");

else {

System.out.println("String is not pollindrome");

}
}

3)String Methods:

package StringInJava;

public class StringMethods {

public static void main(String[] args) {

// TODO Auto-generated method stub

String A=" Monali";

A.charAt(3);

System.out.println(A.charAt(3));

A.indexOf('n');

System.out.println(A.indexOf('n'));

A.substring(2);

System.out.println(A.substring(2));

System.out.println(A.substring(1, 4));

A.substring(1, 4);

System.out.println(A.concat(" Prashant"));

System.out.println(A.trim());
}

4)Why string mutable:

package StringInJava;

public class WhyStingMutable {

public static void main(String[] args) {

// TODO Auto-generated method stub

String a="Hello"; //String literal

String b="Hello"; //this will not create new string and will
refer to 'a' string

//a.concat("World");

//String c=a.concat("World");

//System.out.println(c);

String s=new String("Hello");

String s1=new String("Hello"); //"String class" will creates new


object every time in memory

System.out.println(a.equals(b));

System.out.println(a==b);

System.out.println(a.equals(s));

System.out.println(a==s); //Fail matching the references


System.out.println(s==s1); //fail references are different as
they are defined with "string class"

5)

You might also like