String Class X
String Class X
String
Constructors
Syntax:
String String();
String(char chars[ ])
The constructor can have a String object that contains the same
character sequence as another String object using following
constructor:
String(String strObj)
Methods
LIBRARY
String Length : length()
charAt( )
class anany
{public static void main(String x)
{int m,d=0,b,a;
char c,r;
r=x.charAt(d);
System.out.print(r+".");
int l =x.length();
for(m=0;m<l;m++)
{c=x.charAt(m);
if(c==' ')
{b=m+1;
r=x.charAt(b);
System.out.print(r+".");
}}}}
WAP TO INPUT A STRING AMD PRINT ITS
REVERSE
import java.util.*;
class ReverseString { public static void main(String args[])
{ String original, reverse = ""; Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}}
}
}
Example Output
Basic indexOf() example Char 's' at first occurance: 1 String "this" at first occurance: 4
First occurance of char 's' from 4th index onwards : 7 First occurance of String "this"
from 6th index onwards: 28 -:
wap to print the no. of words and
no of spaces in it
class spacesandwords
{public static void main(String st)
{
int i,spaces=0;
String str=st.trim();
int l =str.length();
for(i=0;i<l;i++)
{char c=str.charAt(i);
if(c==' ')
spaces++;
}
int words=spaces+1;
System.out.println("Number of spaces are "+spaces);
System.out.println("Number of words are "+words);
}}