Você está na página 1de 6

String Java

import java.io.*;

class StringTest

public static void main(String arg[])

DataInputStream dts=new DataInputStream(System.in);

try

System.out.println("Enter a string");

String s1=dts.readLine();

String s2,s3;

int position,index;

char ind;

boolean eql;

System.out.println("Original string: "+s1);

System.out.println("1.Lower Case\n2.Upper Case\n3.Replace\n4.Trim");

System.out.println("5.Equals\n6.Equals Ignore Case\n7.Length”)


System.out.println("8.Char At\n9.Compare To \n10.Concate”);
System.out.println("11.Substring \n12.Value of \n13.To
String"); System.out.println("14.Index of\n15.Index of
Character");

int ch=Integer.parseInt(dts.readLine());

switch(ch)

case 1:
System.out.println("Lower Case");

s2=s1.toLowerCase();

System.out.println("Lower Case is: "+s2);

break;

case 2:

System.out.println("Upper Case");

s2=s1.toUpperCase();

System.out.println("Upper Case is: "+s2);

break;

case 3:

System.out.println("Replace");

System.out.println("Enter a char to be replaced");

char a=(char)dts.readByte();

System.out.println("Enter a char to be replaced with");

char b=(char)dts.readByte();

s2=s1.replace(a,b);

System.out.println("New String is: "+s2);

break;

case 4:

System.out.println("Trim");

s2=s1.trim();

System.out.println("Trimmed String is:"+s2);

break;

case 5:

System.out.println("Equals");
System.out.println("Enter another string to check equal:");

s2=dts.readLine();

eql=s1.equals(s2);

System.out.println("Result is: "+eql);

break;

case 6:

System.out.println("Equals Ignore Case");

System.out.println("Enter another string”);


s2=dts.readLine();

eql=s1.equalsIgnoreCase(s2);

System.out.println("Result is: "+eql);

break;

case 7:

System.out.println("Length");

int len=s1.length();

System.out.println("Length of s1 is: "+len);

break;

case 8:

System.out.println("Char At");

System.out.println("Enter a position");

position=Integer.parseInt(dts.readLine());

char c=s1.charAt(position);

System.out.println("Character at"+position+" in "+s1+" is: "+c);

break;

case 9:
System.out.println("Compare To");

System.out.println("Enter another string to compare:");

s2=dts.readLine();

int cmp=s1.compareTo(s2);

System.out.println("Result is: "+cmp);

break;

case 10:

System.out.println("Concatenate");

System.out.println("Enter another string to concatenate:");

s2=dts.readLine();

s3=s1.concat(s2);

System.out.println("Concatenation is: "+s3);

break;

case 11:

System.out.println("Substring");

System.out.println("Enter a position");

position=Integer.parseInt(dts.readLine());

s2=s1.substring(position);

System.out.println("string at"+position+" in "+s1+" is: "+s2);

break;

case 12:

System.out.println("Value Of");

System.out.println("Enter a Integer");

position=Integer.parseInt(dts.readLine());

String.valueOf(position);
s3=s1+position;

System.out.println("New String is: "+s3);

break;

case 13:

System.out.println("To String");

System.out.println("Enter a Integer");

position=Integer.parseInt(dts.readLine());

String.valueOf(position);

s3=s1+position;

System.out.println("New String is: "+s3);

break;

case 14:

System.out.println("Index of");

System.out.println("Enter a character to check its index");

ind=(char)dts.readByte();

index=s1.indexOf(ind);

System.out.println("Index of"+ind + "in "+ s1 +"is: "+index);

break;

case 15:

System.out.println("Index of Positon");

System.out.println("Enter a position");

position=Integer.parseInt(dts.readLine());

System.out.println("Enter a character to check its index “);


ind=(char)dts.readByte();

index=s1.indexOf(ind,position);
System.out.println("Index of "+ind + " in "+ s1 +" is: "+index);

break;

default:

System.out.println("Wrong Choice");

break;

catch(Exception e)

System.out.println(e.getMessage());