remove first and last occurrence of string java
String: codedecode
input: e
Output: coddecod
public class Demo200 {
public static void main(String[] args) {
String s="codedecode";
char ch='e';
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
for (int i = s.length() - 1; i > -1; i--)
{
if (s.charAt(i) == ch)
{
s = s.substring(0, i) +
s.substring(i + 1);
break;
}
}
System.out.println(s);
}
}
Output:
coddecod
more programs visit http://sateeshm.com/
No comments:
Post a Comment