0% found this document useful (0 votes)
9 views

Lab 1 - String

Uploaded by

MAISARAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 1 - String

Uploaded by

MAISARAH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

STRING EXAMPLE

Java Programming Basic


String
length() - how long character in String Literal
charAt()
indextOf()
toLowerCase()
toUpperCase()
substring()
lastIndextOf()
replace()
valueOf()
Example 1
class textSubstring{ Output:
public static void main(String [] a) {
String t;
t = “Espresso”;
System.out.println(“text: “ + t);
System.out.println(“t.length()“ + t.length());
String sub = t.substring(2,7);
System.out.println(“sub = t.substring(2,7)” + sub);
System.out.println(“sub.length” + sub.length());
System.out.println(“sub.charAt(3)” + sub.charAt(3));
System.out.println(“t.charAt(3)” + t.charAt(3));
sub = t.substring(2);
System.out.println(“sub = t.substring(2)” + sub);
System.out.println(“sub.length” + sub.length());
System.out.println(“sub.charAt(2)” + sub.charAt(2));
}
}
Example 2
String text1 = “JAVA”; Output:
int n = 55;
String text = “programming”;
System.out.println(“This string is “ + text);
System.out.println(text.length());
System.out.println(“The character in index 4 is “ + text.charAt(4));
System.out.println(“The index of character g is “ +
text.indexOf(‘g’));
System.out.println(text.toLowerCase());
System.out.println(text.toUpperCase());
System.out.println(text.substring(3,7));
System.out.println(text.substring(3,7).length());
text1 += text;
System.out.println(text1);
System.out.println(text1.indexOf(‘r’, text1.indexOf(‘r’) + 1));
System.out.println(text.lastIndexOf(‘m’));
System.out.println(text1.replace(‘A’, ‘O’));
String strN = String.valueOf(n);
Example 3
class textSetLength{ Output:
public static void main(String [] args){
StringBuffer buf = new StringBuffer(“Espresso”);
System.out.println(“text : “ + buf);
System.out.println(“buf.length() = “ + buf.length());

String t = buf.toString();
System.out.println(“sub” + t.substring(2,7));

t = buf.append(t.substring(0,7) + “i”);
System.out.println(t);

t = buf.reverse();
System.out.println(buf);
}
}
Example
String text1 = “JAVA”; Output:
int n = 55;
String text = “programming”;
System.out.println(“This string is “ + text);
System.out.println(text.length());
System.out.println(“The character in index 4 is “ + text.charAt(4));
System.out.println(“The index of character g is “ +
test.indexOf(‘g’));
System.out.println(text.toLowerCase());
System.out.println(text.toUpperCase());
System.out.println(text.substring(3,7));
System.out.println(text.substring(3,7).length());
text1 += text;
System.out.println(text1);
System.out.println(text1.indexOf(‘r’, text1.indexOf(‘r’) + 1));
System.out.println(text.lastIndexOf(‘m’));
System.out.println(text1.replace(‘A’, ‘O’));
String strN = String.valueOf(n);
Exercise 1
Write and run a Java program that does the following task:

Tasks Codes
1. Declare a String object named s containing the string
“Show me the meaning of being lonely”

2. Print the entire string

3. Use the length() method to print the length of the string

4. Use the charAt() method to print the first character in


the string.

5. Use the charAt() and the length() method to print the


last character in the string.

6. Use the indexOf() and the substring() method to print


the first word in the string
Exercise 2
Write and run a Java program that enters a 10-digit string as a telephone number.
Extracts the 3-digit phone provider, 3-digit “exchange” and the remaining for the last
4-digit numbers. Print them. A sample run might look like below:

Sample:

Enter 10-digit telephone number: 0199874682


You are entered 0199874682
The phone provider is 019
The exchange number is 987
The remaining number is 4682
The complete telephone number is 019-9874682
Exercise 3
Write and run a JAVA program that inputs a person’s name in the form First Middle
Last and then prints it in the form Last, First M.

Sample:

Input : William Jefferson Clinton


Output: Clinton, William J.

Extra task:
Ask the user to input 3 names (store in an array) then show the last names is
beginning with 'C'

You might also like