Question

Unit 3: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter...

Unit 3: Discussion - Part 2

No unread replies.No replies.

Question

Read the lecture for Chapter 3, and then answer the following:

Chapter 3 introduces us to the String class. It shows some of the methods one can apply to this type of variable (i.e. length(), toLowerCase(), substring(), indexOf(), etc). This class has many more, all described in the Java documentation. Review these methods and present an example of their usage, properly written in Java code. Explain the purpose of your code and show a screen capture of the program running.

Homework Answers

Answer #1

/****************************************StringExample.java********************************/

import java.util.Arrays;

public class StringExample {

   public static void main(String[] args) {

       /*
       * Following method are very useful in the String
       */
       String example = "Hello, How are you?";

       // return the length of string
       System.out.println("String length is: " + example.length());
       // return the string in lower case
       System.out.println("String in lower case: " + example.toLowerCase());
       // return the string in upper case
       System.out.println("String in upper case: " + example.toUpperCase());
       // return the index of char in string
       System.out.println("String index of perticular character: " + example.indexOf('w'));
       // return the char present on index
       System.out.println("Char present on index: " + example.charAt(9));
       // return the substring
       System.out.println("Sub string :" + example.substring(0, 4));
       // split method
       System.out.println("String array splitted by perticular char" + Arrays.toString(example.split(",")));
       // return the char array
       System.out.println("Char array: " + Arrays.toString(example.toCharArray()));
       // return true or false if string matched
       System.out.println("String matched: " + example.equals("Hello"));
       // return true or false if string matched with ignoring upper case or lower case
       System.out.println("String mathed: " + example.equalsIgnoreCase("hello, how are you?"));
       // return true or false if string contains some part of string which is given
       System.out.println("String contains: " + example.contains("Hello"));
       // return true if string matches some part of string
       System.out.println("String match: " + example.matches("are"));
       // to concating the string
       System.out.println("String concated: " + example.concat(" I'm fine!"));

   }
}
/*********************************output************************************/

String length is: 19
String in lower case: hello, how are you?
String in upper case: HELLO, HOW ARE YOU?
String index of perticular character: 9
Char present on index: w
Sub string :Hell
String array splitted by perticular char[Hello, How are you?]
Char array: [H, e, l, l, o, ,, , H, o, w, , a, r, e, , y, o, u, ?]
String matched: false
String mathed: true
String contains: true
String match: false
String concated: Hello, How are you? I'm fine!

Please let me know if you have any doubt or modify the answer, Thanks :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Unit 5: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter...
Unit 5: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter 5 and then answer the following:. Present an ORIGINAL example of the correct use of the printf method. This example must show how integers, doubles and Strings are properly formatted for display. At least one integer, one double, and one String must be printed using printf. You are encouraged to consult the textbook and any other sources you deem relevant, but come up with...
Unit 5: Discussion - Part 1 No unread replies.No replies. Question Read the lecture for Chapter...
Unit 5: Discussion - Part 1 No unread replies.No replies. Question Read the lecture for Chapter 5 and then answer the following:. Among other topics, this week's lecture covered Application Programming Interface (API) and Wrapper classes. This discussion thread will require material from both of them. Show syntactically correct Java code that requests at least one number from the user. The program will store all the numbers the user provides into separate String variables, and it will use the appropriate...
Week 1: Discussion - Case Study Judy* No unread replies.No replies. YOU MUST COMPLETE THIS TO...
Week 1: Discussion - Case Study Judy* No unread replies.No replies. YOU MUST COMPLETE THIS TO PASS THIS CLASS. After carefully reading and studying the content in Chapter 1, read and answer the case study questions and post them to the discussion board. Week 1, Chapter 1 Discussion Case Study- Judy Assignment Instructions: Use critical thinking and apply the information you learned in chapter 6. Answer all the questions completely. Write a well written paragraph to answer each question. Your...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...