String handling functions

 Strings

String Class 

String is a class in java that is final

String in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

String is also a data type in java 

All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created

Example of String

String a="Hello";

String b=new String();

String c=new String("Hello");

 Methods in String Class 

Basic Methods

1. s.concat(String s2)

Concatenates one string onto another. Appends the String s2 to the String s.

Example:

String str1 = new String("Hello ");

String str2 = new String("World");

String str3 = str1.concat(str2);

System.out.println(str3);

The output  is "Hello World": 

 2. s.length() 

Returns the length of the string.

Example:

String str1 = new String("Hello ");

int n=str1.length();

System.out.println("Length="+n); 

The output is Length=5

 3. s.replace(char, char) 

Replaces all occurrences of one character with a different character. 

Example:

String str = “java” , newstr ;

newstr=str.replace(‘a’,’#’);

System.out.println(newstr);

The output is j#v#

4. s.toLowerCase() 

Converts the entire string to lowercase. 

5. s. toUpperCase() 

Converts the entire string to uppercase. 

Example:

String str = new String("This is MiXeD caSE");

String upper = str.toUpperCase();

String lower = str.toLowerCase();

System.out.println("Upper:"+upper);

System.out.println("Lower:"+lower);

The output is 

Upper:THIS IS MIXED CASE

Lower:this is mixed case

6. trim() 

Trims both leading and trailing whitespace from the string.  

Example:

String s1= “  ab  cd  “;      //  There are 6 spaces.

System.out.println("The length before trim:"+s1.length());  // 4 characters + 6spaces=10

s1 = s1.trim();

System.out.println("The length after trim"+ s1.length());  // 4 characters + 2spaces=6

The output is

The length before trim :10

The length after trim :6 

Methods for getting substring

1. s.charAt(int) 

Returns the character at the specified location. 



Example:

String str = new String("This is a String");

char ch = str.charAt(8);

System.out.println("Character at index 8:"+ch);

The output is

Character at index 8:a

2. s.substring(int i) 

Returns the substring from the i-th char to the end

Example:

String str = new String("This is a Java");

String substr = str.substring(10);

System.out.println("Substring from index 10:"+substr);

The output is

Substring from index 10:Java

3. substring(int i, int k) 

Returns the substring of chars in positions from i to k-1

Example:

String str = new String("Java Programming");

String substr = str.substring(5,9);

System.out.println("Substring from index 5 to 9:"+substr);

The output is

Substring from index 5 to 9:Prog

Methods for comparing strings

1. s1.compareTo(String s2) 

Compares two strings. Returns 0 if they are equal, a negative value if the specified string is greater than the string, or a positive value otherwise. 

2. s1.compareToIgnoreCase(String s2) 

Compares two strings. Returns 0 if they are equal, a negative value if the specified string is greater than the string, or a positive value otherwise. This is not case sensitive

3. s1.equals(String s2)

Returns true if the string matches the object.

4. s1.equalsIgnoreCase(String s2)

Returns true if  the string matches the specified string.This is case insensitive 



Example:

String str1 = new String("abc");

boolean result = str1.equals("ABC");                      // Returns false

boolean result = str1.equalsIgnoreCase("ABC");   // Returns true

Searching Strings

1. s1.indexOf(char) 

Searches for the first occurrence of the specified character and return the index at which the character was found, or –1 on failure.

2. s1. indexOf(char, int startindex) 

Searches for the first occurrence of the specified character following the given offset and return the index at which the character was found, or –1 on failure.. 

3. s1.indexOf(String) 

Searches for the first occurrence of the specified string and return the index at which the string was found, or –1 on failure.. 

3.4 StringBuffer Class

StringBuffer class is a mutable class unlike the String class which is immutable. StringBuffer can be changed dynamically. String buffers are preferred when heavy modification of character strings is involved (appending, inserting, deleting, modifying etc).

StringBuffer Constructors

StringBuffer strBuf1 = new StringBuffer("Bob");

StringBuffer strBuf2 = new StringBuffer(100); //With capacity 100

StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16

3.5 StringBuffer Functions

The following program explains the usage of the some of the basic StringBuffer methods like ;

1. capacity()

Returns the current capacity of the String buffer.

2. length()

Returns the length (character count) of this string buffer.

3. charAt(int index)

The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned.

4. setCharAt(int index, char ch)

The character at the specified index of this string buffer is set to ch

5. toString()

Converts to a string representing the data in this string buffer

6. insert(int offset, char c)

Inserts the string representation of the char argument into this string buffer.

7. delete(int start, int end)

Removes the characters in a substring of this StringBuffer

8. replace(int start, int end, String str)

Replaces the characters in a substring of this StringBuffer with characters in the specified String.

9. reverse()

The character sequence contained in this string buffer is replaced by the reverse of the sequence.

10. append(String str)

Appends the string to this string buffer.

11. setLength(int newLength)

Sets the length of this String buffer.

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat