Oop’s Using java


Special String Operations


Download 0.74 Mb.
bet9/12
Sana15.11.2023
Hajmi0.74 Mb.
#1775298
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
OOP’s Using JAVA Module 2

Special String Operations

  • There are 4 types of special string Operations
    • String Literals
    • String Concatenation
    • String Concatenation with Other Data Types
    • String Conversion and toString( )
  • String literal is created by using double quotes.
  • For Example:
  • String s="javaguides";

  • String Concatenation can be done using + operator, which concatenates two strings, producing a String object.
  • You can concatenate strings with other types of data.
  • For Example:
  • String age = “10";

    String s = "He is " + age + " years old.";

    System.out.println(s);

    //Output: He is 10 years old

You can concatenate strings with other types of data.

  • You can concatenate strings with other types of data.
  • String Conversion and toString( ) : valueOf() method

    For Example

    The java string valueOf() method converts different types of values into string. By the help of string valueOf() method, you can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string and char array to string


class Main{
public static void main(String[] args) {
int value=30;
String s1=String.valueOf(value); System.out.println(s1+10);//concatenating string with 10
}
}

String Handling Character Extraction

  • There are several ways by which characters can be extracted from String class object.
  • charAt() -- method is used to extract a single character at an index.
    • char charAt(int index)

Example of “Character Extraction” Program

  • class temp
  • {
  • public static void main(String args[])
  • {
  • String str="Hello";
  • char ch=str.charAt(2);
  • System.out.println(ch);
  • }
  • }

Download 0.74 Mb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling