0% found this document useful (0 votes)
68 views7 pages

Java String and StringBuffer Methods

The document contains Java programs demonstrating various methods of the String and StringBuffer classes, including operations like length, substring, concatenation, and manipulation methods. Additionally, it includes a program that implements user-defined packages, showcasing how to create and import a package. The document is structured with practical exercises and outputs for each program.

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views7 pages

Java String and StringBuffer Methods

The document contains Java programs demonstrating various methods of the String and StringBuffer classes, including operations like length, substring, concatenation, and manipulation methods. Additionally, it includes a program that implements user-defined packages, showcasing how to create and import a package. The document is structured with practical exercises and outputs for each program.

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name :Amanwad Vishnukant .

v class:CO4I’A’

ROLLNO:13

PRACTICAL NO:11&12.

Aim: performing Program for all methods of string class.

public class Demo { public static void main(String[] args)

String str = "vishnukant";

[Link]("Length of the string: " + [Link]());

[Link]("Character at index 7: " + [Link](7));

[Link]("Substring from index 2 to 7: " + [Link](2, 7));

String str2 = " Amanwad";

[Link]("Concatenated string: " + [Link](str2));

[Link]("Index of 'o' in the string: " + [Link]('o'));

[Link]("Last index of 'o' in the string: " + [Link]('o'));


[Link]("Does the string start with 'Hello'? " + [Link]("Hello"));
[Link]("Does the string end with 'World!'? " + [Link]("World!"));

String str3 = "Hello, World!";

[Link]("Are str and str3 equal? " + [Link](str3));

String str4 = "HELLO, world!";

[Link]("Are str and str4 equal ignoring case? " + [Link](str4));

[Link]("Lowercase string: " + [Link]());

[Link]("Uppercase string: " + [Link]());

String str5 = " vishnu ";

[Link]("Trimmed string: " + [Link]());

[Link]("Replacing 'o' with 'x': " + [Link]('o', 'x'));

String[] words = [Link](",");


[Link]("Splitting the string: ");

for (String word : words) {

[Link]([Link]());

[Link]("Does the string contain 'World'? " + [Link]("World"));

Output:

[Link] for all methods of stringbuffer class.

public class Demo2 { public static void

main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Hello");

[Link](" World");

[Link]("After append(): " + stringBuffer);

[Link](5, ", ");

[Link]("After insert(): " + stringBuffer);

[Link](5, 8);

[Link]("After delete(): " + stringBuffer);

[Link](5);

[Link]("After deleteCharAt(): " + stringBuffer);

[Link]();

[Link]("After reverse(): " + stringBuffer); [Link]("Capacity of


StringBuffer: " + [Link]());

[Link]("Length of StringBuffer: " + [Link]());


[Link](5);

[Link]("After setLength(): " + stringBuffer);

[Link]("Character at index 2: " + [Link](2));

[Link]("Substring from index 1 to 3: " + [Link](1,3));

[Link](1, 3, "XYZ");

[Link]("After replace(): " + stringBuffer);

[Link](50);

[Link]("After ensureCapacity(): " + stringBuffer);

}
Name :Amanwad Vishnukant . V class:CO4I’A’

ROLLNO:13

PRACTICAL NO:20

AIM: write a program to implement user defined packages in terms of creating a

new package and importing the same package vishnu; public class Tr

public void display()

[Link]("it is vishnukant package");


}

SOURCE FILE:

import [Link]; class

Pr

public static void main(String args[])

Tr u=new Tr();

[Link]();

OUTPUT:

Common questions

Powered by AI

The StringBuffer class demonstration shows these methods: - `append(" World")` adds ' World' to the end of 'Hello', resulting in 'Hello World'. - `insert(5, ", ")` inserts ', ' at index 5 resulting in 'Hello, World'. - `delete(5, 8)` removes the characters between index 5 and 8, resulting in 'HelloWorld'. - `deleteCharAt(5)` deletes the character at index 5, producing 'Helloorld'. - `reverse()` reverses the sequence to 'dlrooyllEH'. - `capacity()` returns the current capacity (21). - `length()` gives the length (10) of the current string. - `setLength(5)` truncates the string to 'dlroo'. - `charAt(2)` gets the character at index 2, which is 'r'. - `substring(1,3)` extracts a substring from index 1 to 3, resulting in 'lr'. - `replace(1, 3, "XYZ")` replaces characters at index 1 to 3 with 'XYZ', giving 'dXYZo'. - `ensureCapacity(50)` ensures the capacity is at least 50.

The `equals()` method checks if two strings are exactly the same in terms of sequence and case, while `equalsIgnoreCase()` checks if they are the same regardless of case differences. In the document, `str.equals(str3)` comparing 'vishnukant' and 'Hello, World!' returns false due to different content. Similarly, `str.equalsIgnoreCase(str4)` comparing 'vishnukant' with 'HELLO, world!' also returns false because, despite ignoring case, the content differs.

The program manipulates the string 'vishnukant' using various methods: - `str.length()` returns the length of the string, which is 10. - `str.charAt(7)` returns the character at index 7, which is 'a'. - `str.substring(2, 7)` extracts the substring 'shnuk'. - `str.concat(" Amanwad")` concatenates ' Amanwad' to the string. - `str.indexOf('o')` and `str.lastIndexOf('o')` check the first and last occurrence of 'o', both return -1 as 'o' is not present. - `str.startsWith("Hello")` and `str.endsWith("World!")` return false as 'vishnukant' doesn't start or end with the specified strings. - `str.equals(str3)` compares strings 'vishnukant' and 'Hello, World!', returning false. - `str.equalsIgnoreCase(str4)` compares 'vishnukant' and 'HELLO, world!' ignoring case, returning false. - `str.toLowerCase()` and `str.toUpperCase()` convert to lower and uppercase versions of 'vishnukant'. - `str5.trim()` removes leading and trailing spaces from ' vishnu '. - `str.replace('o', 'x')` replaces 'o' with 'x', returning the same string as 'o' is not present. - `str.split(",")` splits the string by commas producing an array with one element 'vishnukant'. - `str.contains("World")` checks if the string contains 'World', returning false.

Implementing user-defined packages, as shown in the document, helps in organizing classes and interfaces by grouping them into a package. It enhances code reusability and maintains a clean namespace to prevent naming conflicts. In the example, the package 'vishnu' is created with class 'Tr', which contains the method `display()`. The main program imports this package and its class to execute the method. This modularity facilitates easier management and distribution of the classes.

The `trim()` method in string manipulation removes leading and trailing whitespace from a string, which can be useful for cleaning up user input. In the case of the string ' vishnu ', using `trim()` returns 'vishnu' by removing the spaces at the beginning and the end of the string.

The `concat()` method in string manipulation merges two strings into a single one. It is significant for combining strings efficiently without creating intermediate objects. In the document, `str.concat(" Amanwad")` is used, attaching ' Amanwad' to 'vishnukant', resulting in 'vishnukant Amanwad'. This operation simplifies the process of string creation and manipulation, as opposed to using operators like `+`, offering clearer semantics, especially for complex concatenations in larger applications.

The `replace` method in the StringBuffer class alters the contents of the current buffer by replacing characters at specified indices with new ones, allowing in-place modifications which are efficient in mutable sequences. In contrast, the String class's `replace` creates a new string since String is immutable, and cannot modify itself. In the document, `stringBuffer.replace(1, 3, "XYZ")` alters part of the current buffer 'dlroo' to 'dXYZo', directly modifying the buffer's state. Meanwhile, for a String, `replace` would generate a new instance encapsulating the altered content.

The `split` method divides a string into an array based on a given delimiter. In the document, `str.split(",")` attempts to split 'vishnukant' using a comma as a separator. Since there are no commas in 'vishnukant', the entire string is returned as a single element in the array. This highlights the method's behavior of returning the full string as a lone entry when the specified delimiter isn't found, which can be critical for parsing or processing delimited data.

If a StringBuffer's capacity is insufficient, it triggers an automatic expansion, which can affect performance, especially in environments where frequent or significant expansions occur. The automatic resizing can lead to increased time complexity due to internal data reallocation and copying. The document uses `ensureCapacity(50)` to preemptively allocate a sufficient amount of storage, minimizing automatic re-expansions and improving time efficiency. This method allows for a smoother performance by reducing the need for repeated capacity reallocations.

The `reverse()` method of the StringBuffer class reverses the sequence of characters in the buffer. For the initial string 'Hello World', after manipulations resulting in 'Hello, World', the application of `reverse()` gives 'dlroW ,olleH'. It changes the order of characters from the last to the first, effectively flipping the string.

You might also like