Home > Java > Java Program to use all the methods defined by string class (substring, indexOf, length, charAt, getchars etc) – Q44

Java Program to use all the methods defined by string class (substring, indexOf, length, charAt, getchars etc) – Q44

January 22, 2009 Leave a comment Go to comments

Q44: Java Program to use all the methods defined by string class (substring, indexOf, length, charAt, getchars etc).

class StringMethods{
	public static void main(String args[]){
	    String s = "This is a demo of the getchars method.";
		int start = 10;
		int end = 14;
		char buf[] = new char[end - start];
		s.getChars(start, end, buf, 0);
	
		System.out.println(buf);
		String org = "This is a test. This is, too.";
		String search = "is";
		String sub = "was";
		String result = "";
		int i;
		
		do{
			System.out.println(" ");
			System.out.println(org);
			i = org.indexOf(search);
			if(i != -1){
				result = org.substring(0, i);
				result = result + sub;
				result = result + org.substring(i + search.length());
				org = result;
			}
		}while(i != -1);
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: