The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Substring Retrieval Method Question

Hows it going guys? Nuggets is back with another simple question for you java gurus here. The question is this.

How would I return a space that has been entered as input using the indexOf method? For example, if someone were to input say "first name", how would I retrieve the space that has been entered in the string?

Re: Substring Retrieval Method Question

I believe you can use the unicode escape '\u00A0' to represent a blank (space) character. Perhaps you should try using that escape.

Also, this line of code is wrong:

String variableName1 = variableName.indexOf();

First of all, the <String>.indexOf() method returns the index of the specified character in the form of an int. You cannot assign that return value to a String. Secondly, when you call <String>.indexOf(), you have to use parameters. Here are all the possible parameters.

Re: Substring Retrieval Method Question

What's the difference between these two then if both return an integer?

public int indexOf(int ch)
Returns the position of the first occurrence of the specified character.

public int indexOf(String str)
Returns the start position of the first occurrence of the specified string.

indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.