Skip to main content

String Methods Java Notes

 Full Explanation Video

String Functions

(i) length() : count the number of characters

***length always starts from 1

(return type is int)

String s = "Rajonya Mandal";

int m = s.length(); 

//include the spaces

m = 14

***index always starts from 0 

(ii) charAt(): If you give the index you will get the character at that position

(return type is char)

String p = "Diksha Kumari";

character index

D           0

i           1

k           2

s           3

h           4

a           5

space       6

K           7

u           8

m           9

a           10

r           11

i           12


(a) char ch = p.charAt(5); 'a'

(b) char ch = p.charAt(p.length()-1);

            = p.charAt(13-1);

= p.charAt(12);

= 'i';

(c) String m = "ABC";

012

char ch = m.charAt(3);

Since index 3 is not present in the string ABC so it will result an error 

StringIndexOutOfBoundsException


(iii) indexOf(): If you pass a character you will get the index of the character present in the string from left to right order, the first occurrence.

(return type is int)

String m = "M a t r i k a   P a u  l";

                0 1 2 3 4 5 6 7 8 9 10 11

(For a single character)

(i) int z = m.indexOf('a'); z=1

(the first occurrence from left to right)

(Between some range)

(ii) int z = m.indexOf('a',2); z=6

(2 is the position from which we look for the first occurence of 'a' )

(iii) int z = m.indexOf("Paul"); z=8

(when we use a word, we first match with the

given sentence,then we look for the first

alphabet's index)

(iv) int z = m.indexOf('j'); z=-1

(whenever a character is not found in the 

string/sentence/word indexOf() gives -1 as the answer)


(iv) lastIndexOf(): This function looks for a character from the ending of a sentence/string/word.

(return type int)

String m = "R a j o n y a   M a n  d  a  l";

index       0 1 2 3 4 5 6 7 8 9 10 11 12 13

(a) int k = m.lastIndexOf('n'); n=10

(v) substring(): This function extracts a certain part of string using the indexes

(return type String)

String m = "D i k s h a   K u m a  r  i     J  a  i  s  w  a  l";

    index       0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

(i) String n = m.subtring(7); "Kumari Jaiswal"

(so the extracted string will taken from index 7 to the very end)

(ii) String n = m.substring(14); "Jaiswal";

(iii) String n = m.substring(7,13); "Kumari"

(7 will be included and the value before 13 will be included which 

means 12)

(iv) String n = m.substring(21); 

Output will be blank  ""

(when the index is not present then it will return blank)

(vi) compareTo() : This function checks two strings lexicographically which means comparision by ascii code.


(return type is int) 


ASCII code 

-----------

'A' - 'Z' : 65 - 90

'a' - 'z' : 97 - 122

'0' - '9' : 48 - 57

' '(space) : 32


(a)   String m = "M o u p i a";

      String n = "M a t r i k a

  (as you can see if we compare m and n the first different alphabet is 

  o and u )

  now o's ascii = 111

      now a's ascii = 97

      (i) int z = m.compareTo(n); 111 - 97 => 14

       (then find the difference betweent the ascii code)

  (ii) int z = n.compareTo(m); 97 - 111 => -14

  

(b)    String  m = "Apple";

       String  n = "apple;

(as you can see , A and a first different alphabet in terms of capital and small)

       (i) int z = m.compareTo(n); 65-97 = -32

now A's ascii = 65

and a's ascii = 97

   (ii) int z = n.compareTo(m); 97-65 = 32

 

(c)   String m = "APPLE";

      String n = "APPLE";

  (i) int z = m.compareTo(n); answer is 0

  (when two strings are completely same then we get 0 as the answer)

 

(vii) compareToIgnoreCase(): This functions checks strings lexicographically without the ascii code 

We compare with alphabet position example

A = 1  B=2 .....Z=26

(return type int)

(a)  String m = "Apple";

     String n = "apple";

int z = m.compareToIgnoreCase(n);   0


(b)  String m = "Diksha";  k=11

     String n = "Dishani"; s=19

     int z = m.compareToIgnoreCase(n); 11-19 => -8

     int z = n.compareToIgnoreCase(m);  19-11 => 8


(viii) trim() : This function removes the preceding and leading spaces of a sentence or word . Excluding the middle space.

(return type String)

String m = "   Diksha  Kumari  Jaiswal   ";

String n = m.trim(); "Diksha  Kumari  Jaiswal"

(ix) equals(): This functions checks two string, return true or false.

If two strings are completely same then it return true.

If anyone of the string's alphabet is different then it return false.

(return type boolean)

(a) String m = "Apple";

    String n = "apple";

boolean b = m.equals(n); false 


(b) String m = "Apple";

    String n = "Apple";

boolean b = m.equals(n); true 

(x) equalsIgnoreCase(): This function returns true when both strings are same if even they may be small letter or capital letter.

(returns boolean)

(a) String m = "Ninja";

    String n = "ninja";

boolean b = m.equalsIgnoreCase(n); true 


(b) String m = "Ninja";

    String n  = "Minja";

boolean b = m.equalsIgnoreCase(n); false 

(xi) toUpperCase(): Converts the entire sentence/word to capital letters.

(return String)

(a) String m = "Shreyan Nag";

    String n = m.toUpperCase();

       n = "SHREYAN NAG";

   

(b) String m = "SNCC #1 Computer Classes";

    String n = m.toUpperCase();

         = "SNCC #1 COMPUTER CLASSES";

 

(xii) toLowerCase(): Convert the entire sentence/word to small letters.

(return String)

(a) String m = "Goldbach Number";

    String n = m.toUpperCase();

         = "goldbach number";

 


(xiii) replace(): It replace a word/character in a string 

(returns string)

(a)  String p = "mimja";

     String n = p.replace('m','n');   ninja


(b)  String p = "Blue Ball Blue Bag";

String n = p.replace("Blue","Green");

          = "Green Ball Green Bag";

(For Class 11 & 12)

String Buffer


1) declaration:

StringBuffer sb = new StringBuffer();


2) Functions:


(a) append(): This functions joins two strings

StringBuffer sb = new StringBuffer("Good");

sb.append("Night");

System.out.println(sb);


Output:

GoodNight


(b) insert(): This function inserts a given string at any given position(index position)


StringBuffer sb = new StringBuffer("Jva");

sb.insert(1,"a") 

System.out.println(sb);


Output:

Java


(c) replace(): This functions replaces a given string between a begining index and ending index-1


StringBuffer sb = new StringBuffer("Python Code");

sb.replace(0,5,"Java");

System.out.println(sb);


Output:

Java Code


(d) delete(): This function deletes a particular strings from the begining index and ending index-1


StringBuffer sb = new StringBuffer("Information");

sb.delete(5,11);

System.out.println(sb);

"Info"


(e) reverse(): This function reverse a particular string.


StringBuffer sb = new StringBuffer("DOG");

sb.reverse();

System.out.println(sb);

"GOD"


(f) capacity(): This function displays the length of string


StringBuffer sb = new StringBuffer();

System.out.println(sb.capacity()); // 16 is the default value

StringBuffer sb = new StringBuffer("GOD");

System.out.println(sb.capacity()); // now 3


// String Tokenizer


Declaration


StringTokenizer str = new StringTokenizer(string value);


// Functions


hasMoreTokens() --> return boolean true/false


use--------

It checks if there is more tokens available


nextToken()  ---> returns string/word


use-------------

It returns the next token from the string tokenizer object.

If only token is present then the current token is shown.


countTokens() ----> it returns total number of tokens.


By default the delimeter for string tokenizer is space " ".


Name,Class,Roll  --> delimeter ,

Name Class ROll ---> delimeter " "



**It it present under java.util package



Popular posts from this blog