1

I am new to Java programing, and I am confused when using the String.IndexOf(int ch) in my code.

For example, I want to find the first occurrence of '-' in a string "abc-def-ghi".

so here is my code:

String test = "abc-def-ghi";
int firstOcc = test.IndexOf('-');

This code is working fine.

However, my question is, why String.IndexOf() function takes a int parameter as a char?

Is there any benefit to writing code as IndexOf(int ch) instead of IndexOf(char ch)?

Thanks for any help!

3
  • 3
    @duffymo - Um, the signature of the method is String.indexOf(int). OP is asking why the arg is int and not char. Commented Jul 18, 2013 at 23:00
  • @Stobor - you are right. I wonder how I didn't see this post before I post my question.... Commented Jul 18, 2013 at 23:14
  • My mistake - I misread it. Commented Jul 18, 2013 at 23:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.