Java Basic Problems

Java:How to insert a word in the middle and at the last position of a String.

Here is the solution to insert a word in the middle and at the last position of a string.

class StringManipulation
{
 public static void main(String args[])
{
StringBuffer str= new StringBuffer(“Object Language”);
System.out.println(“\n”);
System.out.println(“Original String:”+str);
System.out.println(“\n”);
System.out.println(“Length of String:”+str.length());

 for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println(“Character at position:”+p+”:is:”+str.charAt(i));

}
String aString=new String(str.toString());
int pos=aString.indexOf(“Language”);
System.out.println(“\n”);
 
 str.insert(pos,”Oriented”);

System.out.println(“Modified string:”+str);
 str.setCharAt(6,’-‘);
System.out.println(“\n”);
System.out.println(“String now:”+str);
System.out.println(“\n”);
str.append(“improves security”);
System.out.println(“Appended string:”+str);
}
}

OUTPUT:

 insert word in the middle of string

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 )

Facebook photo

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

Connecting to %s