String Operations

String Operations :


Concatenation using '+'.


Position of First 'o' using indexOf().

Position of LastIndex of 'o' using lastIndexOf().

Getting Substring in a String using substring method substring(beginIndex , endIndex);

Example :


/**
 *
 */
package com.techyield.operationsinarray;

public class StringModification {

/**
* @param args
*/
public static void main(String[] args) {

String s1 = "Sunny";
s1+=" G";

System.out.println(s1);

System.out.println("The index of first n : "+s1.indexOf('n'));
System.out.println("The index of last n :"+s1.lastIndexOf('n'));

// Substring

System.out.println(s1.substring(2,7));
}

}

Output :



Sunny G
The index of first n : 2
The index of last n :3
nny G


Notes:

We should be aware of the time complexity of built in operations.

If the length of the string is N , the time complexity of both finding and substring operation is O(N).

Never forget the time complexity of the built-in operations into consideration when you are finding the time complexity of your solution.














No comments:

Post a Comment

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...