0% found this document useful (0 votes)
2 views12 pages

Java Strings.

Uploaded by

lavii2574
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)
2 views12 pages

Java Strings.

Uploaded by

lavii2574
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

1.

Write a Java program to get the character at the given index


within the String.
Pictorial Presentation:

Sample Solution:
Java Code:
public class Exercise1 {
public static void main(String[] args)
{
String str = "Java Exercises!";
[Link]("Original String = " + str);
// Get the character at positions 0 and
10. int index1 = [Link](0);
int index2 = [Link](10);
// Print out the results.
[Link]("The character at position 0 is " +
(char)index1);
[Link]("The character at position 10 is " +
(char)index2);
}
}
Copy
Sample Output:
Original String = Java Exercises!
The character at position 0 is J
The character at position 10 is i
2. Write a Java program to get the character (Unicode code point)
before the specified index within the String.

Sample Solution:

Java Code:

public class Exercise3 {

public static void main(String[] args) {

String str = "[Link]";

[Link]("Original String : " + str);

// codepoint before index 1

int val1 = [Link](1);

// codepoint before index 9

int val2 = [Link](9);

// prints character before index1 in string

[Link]("Character(unicode point) = " + val1);

// prints character before index9 in string

[Link]("Character(unicode point) = " + val2);

Copy
Sample Output:
Original String : [Link]
Character(unicode point) = 119
Character(unicode point) = 99
3. Write a Java program to concatenate a given string to the end
of another string.

Pictorial Presentation:

Sample Solution:
Java Code:
public class Exercise7 {
public static void main(String[] args)
{
String str1 = "PHP Exercises and ";
String str2 = "Python Exercises";
[Link]("String 1: " +
str1); [Link]("String 2:
" + str2);
// Concatenate the two strings together.
String str3 = [Link](str2);
// Display the new String.
[Link]("The concatenated string: " + str3);
}
}
Copy
Sample Output:
String 1: PHP Exercises and
String 2: Python Exercises
The concatenated string: PHP Exercises and Python Exercises
4. Write a Java program to check whether two String objects
contain the same data.
Pictorial Presentation:

Sample
Solution: Java
Code:
public class Exercise13 {
public static void main(String[] args)
{
String columnist1 = "Stephen Edwin
King"; String columnist2 = "Walter
Winchell"; String columnist3 = "Mike
Royko";
// Are any of the above Strings equal to one
another? boolean equals1 =
[Link](columnist2); boolean equals2 =
[Link](columnist3);
// Display the results of the equality checks.
[Link]("\"" + columnist1 + "\" equals \"" +
columnist2 + "\"? " + equals1);
[Link]("\"" + columnist1 + "\" equals
\"" +
columnist3 + "\"? " + equals2);
Copy
Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "Mike Royko"? false
5. Write a java program to print current date and time in the specified
format.
Pictorial Presentation:

Sample Solution:
Java Code:
import [Link];
public class Exercise15 {
public static void main(String[] args) {
Calendar c = [Link]();
[Link]("Current Date and Time :");
[Link]("%tB %te, %tY%n", c, c, c);
[Link]("%tl:%tM %tp%n", c, c, c);
}
}

Copy
Sample Output:
Current Date and Time:
June 19, 2017
3:13 pm
6. Write a Java program to create a unique identifier of a given
string. Hash Code is the memory address location

Sample Solution:

Java Code:

public class Exercise18 {

public static void main(String[] args)

String str = "Python Exercises.";

// Get the hash code for the above

string. int hash_code = [Link]();

// Display the hash code.

[Link]("The hash for " +

str +

" is " + hash_code);

Copy
Sample Output:
The hash for Python Exercises. is 863132599
7. Write a Java program to get the index of all the characters of the
alphabet. Sample string of all alphabet: "The quick brown fox jumps
over the lazy dog." Sample Solution:
Java Code:
public class Exercise19 {

public static void main(String[] args)


{
String str = "The quick brown fox jumps over the lazy dog.";

// Get the index of all the characters of the alphabet


// starting from the beginning of the
String. int a = [Link]("a", 0);
int b = [Link]("b",
0); int c =
[Link]("c", 0); int
d = [Link]("d", 0);
int e = [Link]("e",
0); int f =
[Link]("f", 0); int
g = [Link]("g", 0);
int h = [Link]("h",
0); int i =
[Link]("i", 0); int
j = [Link]("j", 0);
int k = [Link]("k",
0); int l =
[Link]("l", 0); int
m = [Link]("m", 0);
int n = [Link]("n",
0); int o =
[Link]("o", 0); int
p = [Link]("p", 0);
int q = [Link]("q",
0); int r =
[Link]("r", 0); int
s = [Link]("s", 0);
int t = [Link]("t",
0); int u =
[Link]("u", 0); int
v = [Link]("v", 0);
int w = [Link]("w",
0); int x =
[Link]("x", 0); int
y = [Link]("y", 0);
int z = [Link]("z",
0);

// Display the results of all the indexOf method


calls. [Link](" a b c d e f g h i
j"); [Link]("=========================");
[Link](a + " " + b + " " + c + " " + d +
" " +
e + " " + f + " " + g + " " + h + " " +
i + " " + j + "\n");

[Link]("k l m n o p q r s t");
[Link]("===========================");
[Link](k + " " + l + " " + m + " " + n +
" " +
o + " " + p + " " + q + " " + r + " " +
s + " " + t + "\n");

[Link]("u v w x y z");
[Link]("================");
[Link](u + " " + v + " " + w + " " + x +
Copy " " +
Sample Output:
a b c d e f g h i j
=========================
36 10 7 40 2 16 42 1 6 20

k l m n o p q r s t
===========================
8 35 22 14 12 23 4 11 24 31

u v w x y z
================
5 27 13 18 38 37
8. Write a Java program to check whether a given string starts
with the contents of another string.
Pictorial Presentation:

Sample Solution:
Java Code:
public class Exercise26 {
public static void main(String[] args)
{
String str1 = "Red is favorite color.";
String str2 = "Orange is also my favorite color.";
// The String to check the above two Strings to see
// if they start with this value (Red).
String startStr = "Red";
// Do either of the first two Strings start with
startStr? boolean starts1 = [Link](startStr);
boolean starts2 = [Link](startStr);
// Display the results of the startsWith calls.
[Link]( str1 + " starts with " +
startStr + "? " + starts1);
[Link](str2 + " starts with " +
startStr + "? " + starts2);
}
}

Sample Output:
Red is favorite color. starts with Red? true
Orange is also my favorite color. starts with Red? false
9. Write a Java program to create a character array containing the
contents of a string.
Sample Solution:
Java Code:
public class Exercise28 {

public static void main(String[] args)


{
String str = "Java Exercises.";

// Convert the above string to a char array.


char[] arr = [Link]();

// Display the contents of the char array.


[Link](arr);
}
}

Copy
Sample Output:
Java Exercises.
10. Write a Java program to convert all the characters in a
string to lowercase.
Pictorial Presentation:

Sample Solution:
Java Code:
public class Exercise29 {
public static void main(String[] args)
{
String str = "The Quick BroWn FoX!";
// Convert the above string to all lowercase.
String lowerStr = [Link]();
// Display the two strings for comparison.
[Link]("Original String: " + str);
[Link]("String in lowercase: " + lowerStr);
}
}

Copy
Sample Output:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!

You might also like