0% found this document useful (0 votes)
26 views4 pages

String Operations in Java

The document describes a Java program that performs various operations on Strings, including finding length, converting case, concatenating, finding substrings, comparing, and more. The program takes Strings as input from the user, performs the described operations, and outputs the results. It demonstrates the use of many common String methods like length(), toUpperCase(), concat(), substring(), equals(), compareTo(), and more.

Uploaded by

Sarthak Ravat
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)
26 views4 pages

String Operations in Java

The document describes a Java program that performs various operations on Strings, including finding length, converting case, concatenating, finding substrings, comparing, and more. The program takes Strings as input from the user, performs the described operations, and outputs the results. It demonstrates the use of many common String methods like length(), toUpperCase(), concat(), substring(), equals(), compareTo(), and more.

Uploaded by

Sarthak Ravat
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: Sarthak Ravat 196170307128

 Write a program which reads 2 String from user and perform following  operation: 
1. Find string length 

2. Convert one string into uppercase 

3. Convert one string in lower case 

4. Concate string using method 

5. Find string length of new concated string 

6. Use valueOf() method 

7. Find substring from given string 

8. Use indexOf( ) method 

9. Use replace( ) method 

10. Use charAt( ) method 

[Link] String 

12. Use append() method 

13. Use setCharAt( ) method 

Java Practical 1
Name: Sarthak Ravat 196170307128

import [Link]; 
class Demo{ 
public static void main(String args[]){ 
Scanner x=new Scanner([Link]); 
Scanner y=new Scanner([Link]); 
StringBuffer s2=new StringBuffer("Sarthak Ravat"); 
[Link]("Enter First value: "); 
String a=[Link]();

[Link](""); 
[Link]("Enter Second value: "); 
String b=[Link](); 
[Link](""); 
[Link]("[Link] string length"); 
[Link](a+" | "+[Link]()); 
[Link](b+" | "+[Link]()); 
[Link](""); 
[Link]("[Link] one string into uppercase"); [Link](a+
" | "+[Link]()); [Link](b+" | "+[Link]()); [Link].
println(""); 
[Link]("[Link] one string in lower case"); [Link](a+" 
|"+[Link]()); [Link](b+" | "+[Link]()); [Link]
ntln(""); 
[Link]("[Link] string using method"); String c=[Link](b); 
[Link]("After Ans: "+c);
 

[Link](""); 
[Link]("[Link] string length of new concated string"); [Link]
intln(c+" | "+[Link]()); 
[Link](""); 
[Link]("[Link] valueOf() method"); 
int value=30;  
String s1=[Link](value);  
[Link](s1); 
[Link](""); 
[Link]("[Link] substring from given string"); [Link](a+" 
| "+[Link](2)); 
[Link](b+" | "+[Link](3,8)); 
[Link](""); 
[Link]("[Link] indexOf( ) method"); 
[Link](a+" | "+[Link]('a')); 
[Link](b+" | "+[Link]('a')); 
[Link](""); 

Java Practical 2
Name: Sarthak Ravat 196170307128

[Link]("[Link] replace( ) method");
 

[Link]("After Ans: "+[Link]('r','s')); [Link]("After 
Ans: "+[Link]('b','v')); [Link](""); 
[Link]("[Link] charAt( ) method"); 
[Link](a+" | "+[Link](1)); 
[Link](b+" | "+[Link](5)); 
[Link](""); 
[Link]("[Link] append() method"); [Link]("After Ans: "+s
[Link]("")); [Link](""); 
[Link]("[Link] setCharAt( ) method"); [Link](4,'S'); 
[Link]("After Ans: "+s2); 
[Link](""); 
[Link]("[Link] String"); 
[Link]("After Ans: "+[Link]()); 
[Link](""); 
[Link]();
[Link]();

}

Output:

Java Practical 3
Name: Sarthak Ravat 196170307128

Write a program which reads String from user and compares strings  using
equals() ,equalsIgnoreCase( ) ,compareTo() method.

import [Link]; 
class stringcom{
public static void main(String args[]){ 
Scanner ob=new Scanner([Link]); 
[Link]("Enter the First String"); 
String a= [Link](); 
[Link]("Enter the First String"); 
String b= [Link](); 
[Link]("equal, equalIgnorecase and compare String"); [Link]
tln("After Ans: "+[Link](b)); 
[Link]("After Ans: "+[Link](a)); 
[Link]("After Ans: "+[Link](b)); 
[Link](""); 
[Link]();

Output:

Java Practical 4

Common questions

Powered by AI

Using the equals() and equalsIgnoreCase() methods is crucial for precise string comparison in Java. equals() checks if two strings have the exact sequence of characters, ensuring exact matches, which is essential for tasks like password checks or case-sensitive data validation. equalsIgnoreCase() ignores case differences, enabling case-insensitive comparisons. This is useful when the comparison must ignore case variations, such as user input fields or tags in text processing, demonstrating adaptability in handling various textual data formats .

The substring() method in Java is highly effective for extracting specific sections of a string, allowing developers to obtain substrings based on starting and optionally ending indices. This is particularly useful in parsing data from strings that follow structured formats like CSV files or extracting prefixes and suffixes for data validation. It enhances flexibility in processing strings by isolating necessary components without altering the original string, thus maintaining data integrity while offering versatile use in text and data processing applications .

The String.valueOf() method in Java enhances the flexibility of handling numeric values by allowing the conversion of various types of numerals, such as int, float, and double, into their String object representation. This is particularly useful for debugging, logging, and displaying numbers in user-friendly formats. For example, converting an int value of 30 to a String using String.valueOf() simplifies embedding numeric data within text strings, facilitating tasks like concatenation and formatting .

The replace() method is essential for text processing tasks because it simplifies substitution operations, where specific characters or sequences are replaced with alternate values across entire strings. This functionality is invaluable in data cleansing tasks such as removing unwanted symbols from input strings, correcting uniform errors in data, or dynamically adjusting content. By automating these replacements, replace() enhances efficiency and accuracy, ensuring the processed strings meet desired standards and formats .

The Scanner class plays a pivotal role in facilitating user interactions by streamlining input retrieval across various data types, such as String, int, and double from different sources, primarily standard input. Its intuitive methods, like nextLine() and nextInt(), facilitate handling user inputs effectively in both terminal and console applications. By allowing developers to parse input seamlessly and supporting locale-specific formatting, the Scanner class enhances user experience and application reliability in processing real-time data inputs .

The reverse() method in StringBuffer facilitates unique string operations by directly inverting the sequence of characters in the buffer, an operation not available with standard String methods due to their immutability. This method is particularly useful in applications that require palindrome checks or transformations where reversing text structure is necessary. Its in-place modification capability enhances performance and reduces memory usage compared to manually constructing a reversed string using immutable Strings .

setCharAt() and charAt() methods serve contrasting purposes in string manipulation. The charAt() method is used to access a character at a specific index within a String or StringBuffer, facilitating read operations like examinations and validations. Conversely, setCharAt() is exclusive to mutable classes like StringBuffer, allowing modification of the character at a given index, aiding in updates and alterations within the string. This distinctly separates reading from modification tasks, providing control in both examining and changing string content .

Immutability of the String class implies that operations such as lower case conversion result in the creation of a new string object rather than alteration of the existing one. This ensures thread safety and consistent hash codes, crucial in collections like HashMap. However, this characteristic can lead to increased memory usage and slower performance in loops or extensive string manipulation tasks because each operation creates a new object rather than modifying the original one .

The indexOf() and lastIndexOf() methods in Java offer several advantages for string analysis. indexOf() provides the position of the first occurrence of a specified character or substring, allowing for targeted analysis or modification of strings. Conversely, lastIndexOf() returns the position of the last occurrence, which is beneficial when the context or analysis requires any trailing patterns or characters. These methods are particularly useful in parsing strings, data extraction, and avoiding unnecessary processing of the entire string when precise locations of characters are needed .

StringBuffer's append() method is preferred over simple string concatenation in scenarios where performance and memory efficiency are critical. This is because StringBuffer is mutable, meaning it can modify the original object without creating additional objects in memory, unlike immutable String objects, which require creating a new String for each concatenation. This feature is particularly advantageous when concatenating strings inside loops or dealing with large volumes of text, as it reduces memory usage and increases performance .

You might also like