نسخة من Lab01
نسخة من Lab01
String Class
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming
language, strings are treated as objects.
Creating Strings
The most direct way to create a string is to write :
String str = "Hello world!";
As with any other object, you can create String objects by using the new keyword and a constructor. The
String class has 11 constructors that allow you to provide the initial value of the string using different
sources, such as an array of characters.
public class StringDemo {
1 String Length:
returns the number of characters contained in the string object.
public class StringDemo {
2 Concatenating Strings:
string1.concat(string2);
public class StringDemo {
4 int compareTo(Object o)
Compares this String to another Object.
Return Value
The value 0 if the argument is a string lexicographically equal to this string;
a value less than 0 if the argument is a string lexicographically greater
than this string; and a value greater than 0 if the argument is a string
lexicographically less than this string.
public class Test {
Return Value
This method returns a negative integer, zero, or a positive integer as the
specified String is greater than, equal to, or less than this String, ignoring
case considerations.
Return Value
This method returns true if the character sequence represented by the
argument is a suffix of the character sequence represented by this object;
false otherwise. Note that the result will be true if the argument is the
empty string or is equal to this String object as determined by the
equals(Object) method.
Return Value
This method returns true if the argument is not null and the Strings are
equal, ignoring case; false otherwise.
public class Test {
boolean retVal;
}
}
15-
String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the
default locale.
public class Test {
16- void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
public void getChars(int srcBegin, int srcEnd, char[] dst, int
dstBegin)
Parameters
Here is the detail of parameters −
}
} \\ lco
Return Value
It returns a copy of this string with leading and trailing white space
removed, or this string if it has no leading or trailing white space.
20- contains(CharSequence s)
Return Value: true if this string contains the specified sequence of char
values, false otherwise.
Parameters
Here is the detail of parameters −
Return Value
It returns true if the specified subregion of this string matches the specified
subregion of the string argument; false otherwise. Whether the matching is
exact or case insensitive depends on the ignoreCase argument.
Example
import java.io.*;
public class Test {
Output
Return Value :true
Return Value :false
StringBuffer Class
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.
1. class StringBufferExample{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello ");
4. sb.append("Java");//now original string is changed
5. System.out.println(sb);//prints Hello Java } }
1. class StringBufferExample2{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello ");
4. sb.insert(1,"Java");//now original string is changed
5. System.out.println(sb);//prints HJavaello
6. }
7. }
1. class StringBufferExample3{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello studants");
4. sb.replace(1,10,"Java");
5. System.out.println(sb);//prints HJavaents
6. }
7. }
1. class StringBufferExample4{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello students");
4. sb.delete(1,8);
5. System.out.println(sb);//prints Hudents
6. }
7. }
1. class StringBufferExample5{
2. public static void main(String args[]){
3. StringBuffer sb=new StringBuffer("Hello");
4. sb.reverse();
5. System.out.println(sb);//prints olleH }}
exercises
1- Write a Java program to get the character at the given index within the
String.
Sample Output:
Original String = Java Exercises!
The character at position 0 is J
The character at position 10 is i
.Sample Output:
Comparing example.com and example.com: true
7- Write a Java program to get the index of all the characters of the alphabet
Sample Output:
by name of allah
a b c d e f g h i j
=========================
4 0 -1 -1 6 9 -1 15 -1 -1
k l m n o p q r s t
===========================
-1 12 5 3 8 -1 -1 -1 -1 -1
u v w x y z
================
-1 -1 -1 -1 1 -1
8- Write a Java program to get a substring of a given string between two
specified positions.
Sample Output:
old = The quick brown fox jumps over the lazy dog.
new = brown fox jumps
9- Write a Java program to convert all the characters in a string to lowercase.
Sample Output:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
10- Write a Java program to determine if the input phone number is belonged to
zain, umnia or orange network.