0% found this document useful (0 votes)
3 views3 pages

Java String Manipulation Examples

The document contains Java code snippets demonstrating various string manipulation techniques. It includes programs to print even-length words, reverse a string, and add characters to a string at different positions. The methods shown utilize basic string operations and the StringBuffer class for insertion tasks.

Uploaded by

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

Java String Manipulation Examples

The document contains Java code snippets demonstrating various string manipulation techniques. It includes programs to print even-length words, reverse a string, and add characters to a string at different positions. The methods shown utilize basic string operations and the StringBuffer class for insertion tasks.

Uploaded by

arun010takuli
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Java program to print Even length words in a String


class evenlength {
public static void printWords(String s)
{
for (String w : [Link](" ")){
// if length is even
if ([Link]() % 2 == 0)
[Link](w);
}
}

public static void main(String[] args)


{
String s = "My first Java Program. Lets do Programming!";
printWords(s);
}
}

[Link] a String in Java


import [Link].*;
import [Link];
class revStr {
public static void main(String[] args) {
String s = "JAVA PROGRAMMING";
String r = "";
char ch;
for (int i = 0; i < [Link](); i++) {
ch = [Link](i);
r = ch + r;
}
[Link](r);
}
}
[Link] Program to Add Characters to a String
a) At the end
import [Link].*;
public class Main {
public static void main(String args[])
{
char a = 'g';
String str = "Java Programmin";
String str2 = str + a;
[Link](str2);
}
}
b) At the beginning
import [Link].*;
public class Main {
public static void main(String args[])
{
char a = 'J';
String str = "ava Progranmming";
String str2 = a + str;
[Link](str2);
}
}

c) Using insert() method of StringBuffer class


public class InsertExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello World");
[Link](6, "Java ");
[Link]("After insertion: " + [Link]());
}
}

d) At a specific index position


public class AddCharacters {
public static void main(String[] args) {
String original = "HelloWorld";
StringBuffer sb = new StringBuffer(original);
[Link](5, " Java");
[Link]("Modified String: " + [Link]());
}
}

You might also like