1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Java Search String using indexOf Example This example shows how we can search a word within a String object using indexOf method. */ public class SearchStringExample { public static void main(String[] args) { //declare a String object String strOrig = "Hello world Hello World"; /* To search a particular word in a given string use indexOf method. indexOf method. It returns a position index of a word within the string if found. Otherwise it returns -1. */ int intIndex = strOrig.indexOf("Hello"); if(intIndex == - 1){ System.out.println("Hello not found"); }else{ System.out.println("Found Hello at index " + intIndex); } /* we can also search a word after particular position using indexOf(String word, int position) method. */ int positionIndex = strOrig.indexOf("Hello",11); System.out.println("Index of Hello after 11 is " + positionIndex); /* Use lastIndexOf method to search a last occurrence of a word within string. */ int lastIndex = strOrig.lastIndexOf("Hello"); System.out.println("Last occurrence of Hello is at index " + lastIndex); } } /* Output of the program would be : Found Hello at index 0 Index of Hello after 11 is 12 Last occurrence of Hello is at index 12 */ |
i need a program
q: write a program to simulate on-line shopping?
number of occurance?
how to get this ouput from following input
input:String i=”aaabccccdd”;
output:a3bc4d2
public class test
{
public static void main(String[] args)
{
String str = “aaabccccdd”;
int i,j,k;
char[] ch = str.toCharArray();
int len=ch.length;
for ( i = 0; i < len; i++) {
int counter = 0;
char c=str.charAt(i);
for ( j = 0; j < len; j++) {
if (c==ch[j]){
counter++;
ch[j]=’\u0000′;
}
}
if(counter>0)
System.out.println(c+””+counter);
}
}
}
if(intIndex == – 1)
can anyone help me, why we using this?