Java Strings
A String variable contains a collection of characters surrounded by double quotes. Strings in Java are Objects that are backed internally by a char array.
Syntax for declaring a string<String_Type> <string_variable> = “<sequence_of_string>”;
ExampleString str = "RAKESH";
Whenever a String Object is created, two objects will be created- one in the Heap Area and another in the String constant pool and the String object reference always points to heap area object.
The Java string constant pool is an area in heap memory where Java stores literal string values. The heap memory is a run time data area from which the memory for all java class instances and arrays is allocated.
1. Using string literal Example: String s = “java”;
A string written inside double quotes is called string literal. Whenever we create string literal the JVM checks string constant pool. If the string is already present in pool then its reference is fetched. If the string is not present in pool then a new string object is created in the pool.
2. Using new keyword Example: String s = new String(“java”);
When we create string using new keyword it goes to heap.
String Length
Using length method we can found length of a string.
Syntax:type var-name[]=new type[size];
Example1String txt = "RK TUTORIALS";
Example2
System.out.println("The length of the txt string is: " + txt.length());
Example2
import java.io.*; import java.lang.*; public class Mystring { public static void main(String[] args) { String s=new String("Rakesh Kumar"); System.out.println("Length="+s.length()); } }
Output
Length=12
Some useful String Methods
The String class has a set of built-in methods that we can use on strings.
Method | Description | Return Type |
---|---|---|
charAt() | Returns the character at the specified index (position) | char |
contains() | Checks whether a string contains a sequence of characters | boolean |
contentEquals() | Checks whether a string contains the exact same sequence of characters of the specified CharSequence or StringBuffer | boolean |
equals() | Compares two strings. Returns true if the strings are equal, and false if not | boolean |
indexOf() | Returns the position of the first found occurrence of specified characters in a string | int |
isEmpty() | Checks whether a string is empty or not | boolean |
length() | Returns the length of a specified string | int |
substring() | Extracts the characters from a string, beginning at a specified start position, and through the specified number of character | String |
toLowerCase() | Converts a string to lower case letters | String |
toString() | Returns the value of a String object | String |
toUpperCase() | Converts a string to upper case letters | String |
trim() | Removes whitespace from both ends of a string | String |
Example-Find out total length with sapce, without space, number of vowels and consonants of given string.
import java.util.Scanner; import java.lang.*; class vowelcons { public static void main(String[] argv) { Scanner obj=new Scanner(System.in); System.out.println("Enter any String"); String str=obj.nextLine(); int i,c=0,s=0,l,lw,con; l=str.length(); for(i=0;i<l;i++) { if(str.charAt(i)=='a'||str.charAt(i)=='e'|| str.charAt(i)=='i'||str.charAt(i)=='o'|| str.charAt(i)=='u') { c++; } else if(str.charAt(i)==' ') { s++; } } lw=l-s; con=lw-c; System.out.println("Total length with space="+l); System.out.println("Total length without space="+lw); System.out.println("No. of vowels="+c); System.out.println("No. of consonants="+con); } }
Output
Enter any string
Rakesh Kumar
Total length with space=12
Total length without space=11
No. of vowels=4
No. of consonants7
Example-Enter message then print it in shortened from.
import java.util.Scanner; import java.lang.Math; class strsms{ public static void main (String str[]){ Scanner scan = new Scanner(System.in); System.out.println("Type the message to be shortened."); String message = scan.nextLine(); int messageLength = message.length(); int messageVowels = 0; String newMessage = ""; int repeat = 0; int total = 0; if (messageLength < 10) { System.out.println("This doesn't need shortening!"); } else { message = message.toLowerCase(); newMessage = newMessage + message.charAt(0); for (int i = 1; i < messageLength; i++) { if (message.charAt(i)== 'a' || message.charAt(i)== 'e' || message.charAt(i)== 'i' || message.charAt(i)== 'o' || message.charAt(i)== 'u') { if (message.charAt(i-1) == ' '){ newMessage = newMessage + message.charAt(i); } else { messageVowels++; } } else { if (message.charAt(i) != message.charAt(i-1)) { newMessage = newMessage + message.charAt(i); } else { repeat++; } } } total = repeat + messageVowels; System.out.println("Shortened message: " + newMessage); } } }
Output
Type the message to be shortened
Hello how are you
Shortened message: hl hw ar y
0 Comments