indexOf « string « Java Data Type Q&A

1. Java indexOf returns false for unfound string?

I need to know whether in Java does the indexOf() method return false or void for an unfound string? or does it return an index int of 0?

2. is startsWith faster than indexOf

I am writing some java code where I branch off based on whether a string starts with certain characters while looping through a dataset and my dataset is expected to be ...

3. Java String.indexOf API

I have following situation,

String a="<em>crawler</em> <em> Yeahhhhh </em></a></h3><table";
It returns the 11 as the result which was the first that it founds. Is there any way to detect the last instead ...

4. Why is Java String indexOf failing?

this must be quite simple but I am having great difficulty. You see I am trying to find a string within another string as follows.


5. Java String.indexOf and empty Strings

I'm curious why the String.indexOf is returning a 0 (instead of -1) when asking for the index of an empty string within a string. The Javadocs only say this method ...

6. Java indexOf method as a short cut to manual search

I've got the following Java code but List.indexOf() seems to do pretty much the same thing (including returning -1 if not found. Is there any way of passing indexOf() an Object ...

7. Java indexOf and lastIndexOf return -1?? Please Help

In the following code, I am trying to check if an int (converted to String) contains duplicate digits.

public static boolean hasDupes(int n)
    String number = ...

8. Java indexOf method for multiple matches

I had a question about the indexOf method. I want to find multiple cases of "X" in a string. Suppose my string is "x is x is x is x", I want ...

9. Wrong output when searching with indexOf

 int locsPlusLast = locsPlus.get(locsPlus.size()-1);
          int index = checkScanning.indexOf('(',locsPlusLast);

10. How to find String.indexOf("\")?

I need to get the substring that appears before a bunch of \r\n\r\n junk. So I want to geto the first /that appears. indexOf("\") is not accepted. String.indexOf("\") ...

11. Trimming a java string based on string values?

I have a database query that returns some string data in a column. I would like to trim this field based on whether a particular string exists in the data? If ...

12. How to Use Indexof()method in java

I am using indexof() for finding a sub string. It finds my string if my string is not on first position, if my string is at the first location it won't ...

13. indexOf on CharSequence implementations that support it

In my program I use a lot of Strings and StringBuilders. I would like to get rid of the StringBuilder toString() method and use CharSequences throughout. However I need access to ...

14. How to use indexOf and lastIndexOf?

I have an HTML source code in one String. I want to get a part of string using indexOf and lastIndexOf, but I only get integer

HttpClient client = new DefaultHttpClient();
HttpGet ...

15. Using indexOf() in an if statement

Hi, In one of my JSPs I am trying to compare a character to a string to see if there is an occurance, if so, I display an Input text box in html if not process the else for a different string as such-- <% if (" A&J".indexOf(lL200C.getMc__Commit__Until__Dt__A()) > 0) { %> ...

16. indexOf(str)

Roughly all that method does is steps through the string as an array of characters and stops when it finds the specified character pattern. How quickly it performs is very dependant on the String you are testing, for example looking for "ab" in "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaba" will probably be far slower than in "cccccccccccccccccccccccccccccccabc" since every character in the first string looks like ...

17. Using indexOf(Object o) for String[]

I suppose one thing that might help me understand this is to know what classes are inherited when a String array (or any kind of array excluding ArrayLists) is created. I am positive that I have used some method (I don't remember it being the indexOf method) to find the index of an object within an array. For some reason, when ...

18. ++ with String.indexOf()

Hi George, Think operator precedence. The post and pre fix operators have the highest precedence. In your first examples the compiler would need to increment a string using the post fix or pre fix notation both of which are not overloaded for use with a String! "string"++; // Illegal! ++"string"; // Illegal! In the one that does work the method is ...

19. use of String.indexOf(String)

20. indexOf() of String

21. Sun String IndexOf method API Question

indexOf public int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index (in Unicode code units) of the first such occurrence is returned. For values of ch in the range from 0 to 0xFFFF (inclusive),... ...

22. Is indexOf() in String class expensive operation?

You could run some tests and find out for yourself. But I can't imagine how a regular expression that simply searched for an "a" in a String could avoid looking at all the characters that the indexOf() method would look at. If anything I would expect the regex would be slower, because of the setup processing.

23. indexOf ()

25. indexOf implementation in Java

Hi Henry, I think I am allowed to a character by character comparison in a nested for loop. I don't think this qualifies a multi-character comparison, right. I am thinking of having 2 for loops and using break(); once a match is found (Note that I can't use any exit() calls). Thanks Sri

26. String indexOf method

27. doubt in List.indexOf()

29. indexOf

Hi i was looking a for particular set of words is string using indexOf. Below is my code String prefix1 = "Private India's Electronic Components, Inc "; String prefix2 = "Private India's Electronic Components inc"; String prefix3 = "Private India's Electronic Components"; String prefix4 = "Private India's Electronic Components and Semiconductors"; String prefix5 = "Private India's Electronic Components And Semiconductors"; String ...

32. indexof chek

Hi! I'm trying to see if the email that is being typed is valid, so I'm using a indexOf and the question is how to find if the method indexOf has found the typed character in it ? For example . email.indexOf('@', 1); How can I find, if the (email) string contains @, if not to do something, and if doest ...

33. indexOf() error

hello once again... i have a problem with my indexOf method.... i dont know why... it said in the API i can use it with the util. package.. but it doesn't run... can anyone help me please??? its at line 67... codecodecode: public static void main (String args[]) { String[] employees=new String [5]; LinkedList link=new LinkedList(); for (int i=0; i

34. Confusion about indexOf method

35. Printing indexOf line

36. How to string.indexOf for multiple strings and return first instance?

Basically, I am searching a string containing html. And I want to find the first instance of "

37. Am I supposed to use the indexOf method?

import java.util.*; import java.lang.String; public class CheckingSubstring { public static void main(String[] args) { Scanner input = new Scanner(; System.out.print("Please enter a String: "); String string1 =; System.out.print("Please enter a second String: "); String string2 =; if (isSubstring(string1, string2)) { System.out.println("The first string is a substring of the second."); } else { System.out.println("The first string is NOT a substring ...

38. Help Using indexOf()

I am trying to learn and figure out how to use indexOf() for a program I am doing. Can someone help me understand how it is used and how to get the output data that I am looking for. I have SQL that returns the following price list for certain customers: Example List: HAC Retail Price - Manual or HCN Wholesale ...

39. indexOf question

Hi, This may be a dumb question, but can the indexOf method be used in things other than strings. For example, I have an arraylist filled with vertices and I want to find the specific location of a vertex. Would this be allowed: int a = nodeArray.indexOf(vertex) where a would be the location of the particular vertex.

40. string searches, indexof()

.indexOf("o") >-1){ System.out.println("o"); then "o" will also be printed because "o" is in "foot" , i want the code to search for the largest word then work its way down (if that makes any sense) i want my output 2 be foot ball without else if and only using IF i get foot ball o thanks for the reply

41. indexOf, println

I have this program that prints words in a sentence written in JTextField, when i press enter the words are outputted in cmd but no in the order i have written it in. e.g. in my code if i type Hello world, the print comes out world Hello because i coded the world before hello. So is there a possible way ...

42. tricky indexOf implementation -- Help!!

44. question on IndexOf()

45. Help with indexOf()

Well, you're starting at zero, and looping forward until you either find the character or reach the end of the string, right? You have two variables -- "find" and "lookup" -- but it's not clear which if either is meant to be an index for that loop. You really don't need two extra local variables for this. You just need a ...

46. Strings indexOf

public boolean findText(String searchText) { // find text in main area boolean found = false; // initialise find result int startSelection = 0; int endSelection = 0; int searchTextLength = searchText.length(); if (searchTextLength > 0) getText(); while (textPosition >= 0){ // textPosition initialised to 0 at top of program textPosition = textWords.indexOf(searchText, textPosition); if (textPosition >= 0){ startSelection = textPosition; // ...

47. indexof, is it an error or what?

48. found: CharSequence required: String String.indexOf()

The point is that simply replacing the failed assignment with a println doesn't answer the OP's question, and doesn't >help him solve his problem--or at best is a hacky workaround. Thanks jverd, I just used the println because I did not had the full code to display the output... I agree i was wrong....but I am just wondering, does that affects ...

49. indexOf

50. Indexof and lastIndexOf question

In java list api you will get this method: indexOf what it do: public int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no ...

51. .indexOf problem with String

52. Clear and understandable/Regular Exp / Indexof() Issue

If you look at the Regular expression that i have in my code. It says. any 10 digits from 0 to 9, left of consecutive 35 zeros. Which means you are actually counting backwards to get those numbers . Form right to left, starting with first zero of consecutive 35 zeros. that 56 you are talking about is actually the one ...

54. String.indexOf() for DoubleQuotes?

Hi, I have .txt which does have some data such as :- DBLocation=............. DBHost=------------- DBUser="sample" Here i need to search for the word DBUser and get the user data "sample". Where i have used StringindexOf() to get search for DBuser and get the index for that value which is working fine. But inorder to get the whole sentence is the line ...

55. problem with indexOf ()

Hi, I have a strange problem with this method. I have a some String like this: ":8547:Yog.Mora 200g:5123:0.4:0.5:123456789" ":8548:Yog.Mora 220g:5123:0.8:0.9:123456789" ":8549:Yog.Mora 250g:5123:1.0:1.2:123456789" ok now I use indexOf to find when other Strings like this ones has a certaing substring on it. for example the substring "Yog." and works, but the problem is: Searching for: "8547" works perfectly. Searching for: "547" doesnt ...

56. Using indexOf to find multiple instances?

57. indexOf problem

Hi Just See this this is working fine import*; class Files { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(; BufferedReader br = new BufferedReader(isr); String s = null; System.out.println("Enter the name of company or date of order"); File file = new File("month.txt"); FileInputStream fis = null; BufferedInputStream bis = null; BufferedReader bufferedReader = null; ...

58. list.indexOf

Guys, I have a list with Object type empl: long id, //unique String name; int level. I would like to get the index of empl with id X. Q: do I have to overwrite the hashCode? what makes the empl unique? , how can I get the index by the id? thanks in advance

59. Problem with indexOf() method

I'm facing a problem while trying to format a string according to my criteria. I need to make a substring of the string giving the position using indexOf method. But the method isn't working well as I need. Given below is the code....... temp1 = 3O|1||V06080000210R^A^04|^^^106^HBsAg^UNDILUTED|R||||||||||||||||||||F61 I need to cut the string from ^'s 6th position+1 until 7th position-1. I'm using ...