0% found this document useful (0 votes)
95 views10 pages

17BCE0552 Java DA1 PDF

The document discusses various string handling classes and methods in Java - String, StringBuffer, and StringTokenizer. The String class represents a string and provides many methods like compare(), concat(), equals(), split() etc. to perform operations on strings. The StringBuffer class is similar but mutable, for modifying strings. It has methods like append(), insert(), replace() etc. The StringTokenizer class breaks a string into tokens using a delimiter and has methods like hasMoreTokens(), nextToken() to retrieve the tokens.

Uploaded by

ABHIMAYU JENA
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)
95 views10 pages

17BCE0552 Java DA1 PDF

The document discusses various string handling classes and methods in Java - String, StringBuffer, and StringTokenizer. The String class represents a string and provides many methods like compare(), concat(), equals(), split() etc. to perform operations on strings. The StringBuffer class is similar but mutable, for modifying strings. It has methods like append(), insert(), replace() etc. The StringTokenizer class breaks a string into tokens using a delimiter and has methods like hasMoreTokens(), nextToken() to retrieve the tokens.

Uploaded by

ABHIMAYU JENA
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
You are on page 1/ 10

Digital Assignment 1:

Submitted by:
17BCE0552
Vaibhav Ranjan

Course Code: CSE1007


Course Title: Java Programming

Under the guidance of

Dr. ELANGO N M
Associate Professor, SITE,
VIT, Vellore
String Class
In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string. Java String class provides a lot of methods to perform
operations on string such as compare(), concat(), equals(), split(), length(), replace(),
compareTo(), intern(), substring() etc.

Methods in String Class


1. charAt()
import java.util.*;
public class Main {
public static void main(String[] args) {
String str = "Welcome to my world!";
System.out.println("Character at index 0 is: "+ str.charAt(0));
}
}

2. compareTo()
import java.util.*;
public class Main {
public static void main(String args[]) {
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
String s5="flag";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s4));
System.out.println(s1.compareTo(s5));
}
}

3. concat()

import java.util.*;
public class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Vaibhav";
String str3 = str1.concat(str2);
System.out.println(str3);
}
}
4. contains()

class Main {
public static void main(String args[]) {
String name="what do you know about me";
System.out.println(name.contains("do you know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}
}

5. equals()

import java.util.*;
public class Main {
public static void main(String args[]) {
String s1="vaibhav";
String s2="vaibhav";
String s3="VAIBHAV";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
StringBuffer Class
Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer
class in java is same as String class except it is mutable i.e. it can be changed.
StringBuffer class is a thread-safe, mutable sequence of characters.

Methods in StringBuffer Class


1. append()

import java.util.*;
class Main {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Vaibhav");
System.out.println(sb);
}
}

2. insert()

import java.util.*;
class Main {
public static void main (String args[]){
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1,"Vaibhav");
System.out.println(sb);
}
}
3. replace()

import java.util.*;
class Main {
public static void main(String args[]) {
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Vaibhav");
System.out.println(sb);
}
}

4. delete()

import java.util.*;
class Main {
public static void main(String args[]) {
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);
}
}
5. reverse()

import java.util.*;
class Main {
public static void main(String args[]) {
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}

StringTokenizer Class
The StringTokenizer class allows you to break a string into tokens. It is simple way to
break string. It doesn't provide the facility to differentiate numbers, quoted strings,
identifiers etc.

Methods in StringTokenizer Class


1. hasMoreTokens()

import java.util.*;
public class Main {
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer("My name is Vaibhav"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}

2. nextToken()

import java.util.*;
public class Main {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my,name,is,khan");
System.out.println("Next token is : " + st.nextToken(","));
}
}

3. hasMoreElements()

import java.util.*;
public class Main {
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer("My name is Vaibhav"," ");
while (st.hasMoreElements()) {
System.out.println(st.nextToken());
}
}
}

4. countTokens()

import java.util.*;
public class Main {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("My name is Vaibhav Ranjan.");
System.out.println("Total tokens : " + st.countTokens());
}
}

5. nextElement()

import java.util.*;
public class Main {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("Come to learn");
System.out.println("Next element is : " + st.nextElement());
}
}

You might also like