Java Lecture 8 Strings in Java | Strings methods
THE ROYAL GROUP OF PAK
Java Lecture 8 (Strings in Java)
In this Lecture, discuss strings in java. We're explain Strings in java from basic to advance and get command of java Strings methods
A String
variable contains a collection of characters surrounded by double quotes:
Methods:
String Length:
A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the
length()
method:toUpperCase():
Converts whole string into upperCase characters with toUpperCase() method
toLowerCase():
Coverts whole String into smallCase characters with toLowerCase() method
Finding a Character in a String
The
indexOf()
method returns the index (the position) of the first occurrence of a specified text in a string (including whitespace):String Concatenation
The
+
operator can be used between strings to combine them. This is called concatenation:You can also use the
concat()
method to concatenate two strings:Special Characters
Because strings must be written within quotes, Java will misunderstand this string, and generate an error:
String txt = "We are the so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash escape character.
see escape characters in source code
Watch Lecture 5 now 👇
This is the source code we learn in lecture 5
// Index of a string starts from 0
// escapCh Result Description
/* \' ' Single Quote
\" " Double Quote
\\ \ BackSlash
*/
public class RGP_08_Strings {
public static void main(String[] args) {
String greeting = "Good Morning!";
String info = "This is a Java Course";
System.out.println(info);
// length()
System.out.println(info.length());
// toUpperCase()
System.out.println(info.toUpperCase());
// toLowerCase()
System.out.println(info.toLowerCase());
// indexOf()
System.out.println(info.indexOf("i"));
// Concatenation()
System.out.println(greeting + " " + info);
System.out.println(greeting.concat(info));
String str = "C:\\Users\\Zahid\\Videos\\Captures";
System.out.println(str);
String text = "It\'s a Pen";
System.out.println(text);
String txt1 = "Please locate \"Zahid\" where it occurs";
System.out.println(txt1);
}
}
Comments
Post a Comment