java - Why do I keep getting String out of Range? -


return namestring.substring(namestring.indexof(" ", 0), namestring.lastindexof(" ", 0)); 

why keep returning error? want return string first occurring space character, last occurring space in string?

lastindexof

public int lastindexof(string str,                        int fromindex) 

returns index within string of last occurrence of specified substring, searching backward starting @ specified index... if no such value exists, -1 returned.

get rid of , 0 parameters indexof , lastindexof. when pass fromindex of 0 lastindexof searches backwards start of string find match. when doesn't find 1 returns -1 invalid argument substring.

return namestring.substring(namestring.indexof(" "), namestring.lastindexof(" ")); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -