Skip to main content

String Class in Java (Part 1)

>

The String Class: (Part 1)

  • The String class is under Lang package in Java. But as this package is by-default imported in Java, there is not need to write the import statement to use String in your program.
  • The char type represents only one character. To represent a string of characters, we can use the data type called String. For example, the following code declares message to be a string with the value "Welcome to Java".
  • String message = "Welcome to Java";

  • A String object is immutable: Its content cannot be changed once the string is created.

Creating Strings:

  • A String is combination of one or more characters. There are three methods to create Strings:
    1. By creating object of String class:
      • Syntax:
        String object_name = new String(“any_string”);
      • For example:
        String name = new String(“Mr. Harvey Specter”);
    2. By creating a String variable or literal:
      • Syntax:
        String var_name = “Value”;
      • For example:
        String name = “Mr. Mike Ross”;
    3. From character array:
      • Syntax:
        String name = new String(Name_Of_Char_Array);
      • For example:
        char value[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};
        String name = new String(value);

Simple Methods of String class:

  • Example: (Length)
    class test {
    public static void main(String ar[]) {
    String str = "Welcome to Java!";
    System.out.println(str.length()); // prints 16 (space included)
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (charAt - Part 1)
    class test {
    public static void main(String ar[]) {
    String str = "Welcome to Java!";
    System.out.println(str.charAt(3)); // prints c
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (charAt - Part 2)
    class test {
    public static void main(String ar[]) {
    String str = "Welcome to Java!";
    for(int i=0;i<str.length();i++)
    {
    System.out.println(str.charAt(i)); // prints all the characters in the string
    }
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (concat)
    class test {
    public static void main(String ar[]) {
    String str1 = "Welcome to Java ";
    String str2 = "world!";
    String str3 = str1.concat(str2); // prints Welcome to Java world!
    System.out.println(str3);
    String str4 = str2.concat(str1);
    System.out.println(str4); // world!Welcome to Java
    }
    } // spot the diff between str3 and str4
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (toUpperCase)
    class test {
    public static void main(String ar[]) {
    String str1 = "Welcome to Java ";
    String str2 = str1.toUpperCase();
    System.out.println("Original: "+ str1); // prints Original: Welcome to Java
    System.out.println("New: "+ str2);// prints New: WELCOME TO JAVA
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (toLowerCase) (Opposite to toUpperCase())
  • Example: (trim)
    class test {
    public static void main(String ar[]) {
    String str1 = " Welcome to Java ";
    String str2 = str1.trim();
    System.out.println("Original:"+ str1);// prints Original: Welcome to Java
    System.out.println("After trim:"+ str2);// prints After trim:Welcome to Java
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
Click HERE for the Comparison Methods of String class.

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Java's Magic (Byte codes)

> What is JVM? What are Bytecodes (Java’s Magic): JVM is an abbreviation for Java Virtual Machine, which is a set of tools used to execute Java programs. JVM serves as the intermediary between your program and the host computer. Running a java program is a two-step process: With the compiler, first you translate a program into an intermediate language called Java bytecodes—the platform-independent codes interpreted by the interpreter on the Java platform. The interpreter parses and runs each Java bytecode instruction on the computer. Compilation happens just once; interpretation occurs each time the program is executed. Figure: Programs written in the Java programming language are first compiled and then interpreted. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for bytecode. Java bytecodes...

Character Class in Java (Part 2)

> The Character Class: (Part 1) Methods of Character class: Example: Method -> isDigit() [validating name] Example: Method -> isLetter() [validating mobile number] Example: Method -> isLetterOrDigit() [validating user_id / username] Example: Method -> isLowerCase() Example: Method -> isUpperCase() Example: Method -> toUpperCase() and toLowerCase() Click HERE for the list of Methods of Character class. Click HERE for the ASCII Table for different Characters