Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#My String Class

package mystring;
public class MyString {
char[] str;
private String index;

MyString (String a){


str=a.toCharArray();
}
public int length() {
return str.length;
}
public void replace(char oldChar, char newChar) {
for(int i=0;i<str.length;i++) {
if(str[i]==oldChar) {
str[i]=newChar;
}
}
}
public char charAt(int i) {
if(i>=0&&i<str.length) {
return str[i];
}
else {
throw new IndexOutOfBoundsException("Index Not found");
}
}
public boolean contains(String s) {
String a = new String(str);
return a.contains(s);
}
public boolean equals(String s) {
return new String(str).equals(s);
}
public int indexOf(char ch) {
for (int i = 0; i < str.length; i++) {
if (str[i] == ch) {
return i;
}
}
return -1 ;
}
public String toUpperCase() {
return new String(str).toUpperCase();
}
public String toLowerCase(){
return new String(str).toLowerCase();
}

public static void main(String[] args) {


MyString myStr =new MyString(" Jeba Tabassum Urbi ");
System.out.println("Length: " + myStr.length());
System.out.println("Original String: " + new String(myStr.str));
myStr.replace('a', 'x');
System.out.println("After replacing: " + new String(myStr.str));
int index= myStr.indexOf('J');
System.out.println("Index of 'j': " + index);
System.out.println("To Uppercase:" + myStr.toUpperCase().toString());
System.out.println("To Uppercase:" + myStr.toLowerCase().toString());
}
}
Output:

You might also like