0% found this document useful (0 votes)
5 views5 pages

String Class X

Uploaded by

wecomputerdotcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

String Class X

Uploaded by

wecomputerdotcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TRING THEORY + SYNTAX

String

Constructors

To create an empty String, call the default constructor.

Syntax:

String String();

Ex, String s = new String();

Here, it will create an instance of String with no characters in it.

The String class provides a variety of constructors to handle this. To


create a String initialized by an array of characters, use the constructor
shown here:

String(char chars[ ])

Here, the constructor has the char array as an argument.


It is possible to specify a subrange of a character array as an initializer
using the following constructor:

String(char chars[ ], int startIndex, int numChars)

Here, startIndex specifies the index at which the subrange begins,


andnumChars specifies the number of characters to use.

The constructor can have a String object that contains the same
character sequence as another String object using following
constructor:
String(String strObj)

Here, strObj is a String object.

Methods
LIBRARY
String Length : length()

The length of a string is the number of characters that it contains. To


obtain this value, the length( ) method is used to find length of a string.
Syntax:
int length( )

charAt( )

To extract a single character from a String, the charAt( ) method is used.

char charAt(int where)


Here, where is the index of the character that you want to obtain. The
value of where must be nonnegative and specify a location within the
string.charAt( ) returns the character at the specified location.
WAP to accept a string and print
its abbreviated form

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);
}}

Below method shows how to get index of a


specified character or string from the given string.
By using indexOf() method you get get the
position of the sepcified string or char from the
given string. You can also get the index strting
from a specified position of the string.
package com.myjava.string;

public class MyStringIndexOf {

public static void main(String[] a){

String str = "Use this string for testing this";


System.out.println("Basic indexOf() example");
System.out.println("Char 's' at first occurance: "+str.indexOf('s'));
System.out.println("String \"this\" at first occurance: "+str.indexOf("this"));
/**
* Returns the first occurance from specified start index
*/
System.out.println("First occurance of char 's' from 4th index onwards : "
+str.indexOf('s',4));
System.out.println("First occurance of String \"this\" from 6th index
onwards: "
+str.indexOf("this",6));

}
}

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);

}}

Write a Program to accept a String and


print it in following manner
Input - mahendra singh dhoni ( Just an
Example)
Output - M.S.Dhoni

Find the Palindrome String


import java.io.*;
class palindrom
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter a String ");
String s = br.readLine();
int l=s.length();
String x = "";
int ch;
ch=0;
for(int i=l-1;i>=0;i--)
{
x=x+s.charAt(i);
}
if(x.compareTo(s)==0)
System.out.println(" its A palindrom");
else
System.out.println(" its not a palindrom ");
}
}

You might also like