I have a string in java, of uncertain length, and I need to take the first 3 and last 3 characters and put them into new strings. Is there a simple way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Funny, all solutions are buggy (update: except the one with the regex) and may result in
StringIndexOutOfBoundsExceptionwhen the input string’s length is less then 3 (the question explicitly says the length is uncertain). Assuming that str is not null, the proper way would be: