%%js
<!-- Heading -->
<h1 style="color: white; font-family: Arial, sans-serif; background-color: black;">Strings in Java</h1>


<!-- Explanation Section -->
<p style="font-family: Arial, sans-serif; line-height: 1.6; color: white; background-color: black;">
A <strong>string</strong> is an ordered sequence of characters. In programming, a string can contain letters, numbers, spaces, and even special characters. Strings are used to represent text in many programming languages.
</p>


<p style="font-family: Arial, sans-serif; line-height: 1.6; color: white; background-color: black;">
Strings can also have procedures and methods that perform operations like finding their length, combining multiple strings (concatenation), or extracting parts of them (substrings).
</p>


<!-- len() Example -->
<h3 style="font-family: Arial, sans-serif; color: white; background-color: black;">
Example 1: Finding the Length of a String using <code>length()</code> in Java
</h3>
<p style="font-family: Arial, sans-serif; color: white; background-color: black;">
In Java, we can use the <code style="background-color: black; color: white;">length()</code> method to find out how many characters a string contains. This includes letters, spaces, and numbers.
</p>


<pre style="background-color: black; padding: 10px; border-radius: 4px; color: white;">
<code>
public class StringLengthExample {
public static void main(String[] args) {
String example = "APCSP";
// Finding the length of the string
int length = example.length();

// Output the result
System.out.println("The length of the string \"APCSP\" is: " + length);
}
}
</code>
</pre>


<p style="font-family: Arial, sans-serif; color: white; background-color: black;">
In this example, <code style="background-color: black; color: white;">part1 + part2</code> merges the strings <code style="background-color: black; color: white;">"AP"</code> and <code style="background-color: black; color: white;">"CSP"</code> into one string, resulting in <code style="background-color: black; color: white;">"APCSP"</code>.
</p>

<IPython.core.display.Javascript object>
%%javascript
<!-- Heading -->
<h1 style="color: white; font-family: Arial, sans-serif; background-color: black;">Popcorn #4 Hack</h1>


<!-- Body Text Explanation -->
<p style="font-family: Arial, sans-serif; line-height: 1.6; color: white; background-color: black;">
   This hack will help you practice string manipulation in Java. You will:
</p>


<ul style="font-family: Arial, sans-serif; line-height: 1.6; color: white; background-color: black;">
   <li>Find the number of characters in your last name using <code style="background-color: black; color: white;">length()</code>.</li>
   <li>Concatenate your first and last name together using the <code style="background-color: black; color: white;">+</code> operator.</li>
   <li>Use the <code style="background-color: black; color: white;">substring()</code> method to extract characters from the 3rd to the 6th position of your concatenated name.</li>
</ul>


<!-- Java Code for the Hack -->
<h3 style="font-family: Arial, sans-serif; color: white; background-color: black;">Java Code:</h3>


<pre style="background-color: black; padding: 10px; border-radius: 4px; color: white;">
<code>
public class PopcornHack4 {
   public static void main(String[] args) {
       // Find the number of characters in your last name
       String lastName = "Bharadwaj";
       int length = lastName.length();
       System.out.println("The number of characters in your last name is: " + length);


       // Concatenate your first and last names together
       String firstName = "Aditi";
       String fullName = firstName + lastName;
       System.out.println("Your full name is: " + fullName);


       // Use substring to show only the 3rd to 6th characters (index 2 to 6)
       String substring = fullName.substring(2, 6);
       System.out.println("Substring (3rd to 6th characters): " + substring);
   }
}
</code>
</pre>


<p style="font-family: Arial, sans-serif; color: white; background-color: black;">
   In this example, <code style="background-color: black; color: white;">length()</code> is used to find the number of characters in the last name, <code style="background-color: black; color: white;">+</code> is used to concatenate the first and last names, and <code style="background-color: black; color: white;">substring()</code> extracts the 3rd to 6th characters of the concatenated string.
</p>

<IPython.core.display.Javascript object>