By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,646 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,646 IT Pros & Developers. It's quick & easy.

Having issue while extracting required part from a string

P: 4
I have a string, and i want a specific part from that. String is: "Hello how are you doing." I want 4 letter after word "how" and 4 letter after word "you". Also want to add additional string in front that is Joy.
So my final output should be like, "Joyare doi"
Aug 22 '13 #1
Share this Question
Share on Google+
3 Replies

Expert 2.5K+
P: 3,112
Hi chetan and welcome to!
I'm a little confused; you say you want the 4 letters after "how" but then "how" is in your answer. Should the answer maybe be "Joy are doi"?
Aug 22 '13 #2

P: 4
Sorry for confusion.. Answer should be look like:
"Joyare doi"
Aug 23 '13 #3

Expert 2.5K+
P: 3,112
OK, you'll need two functions from the String class: indexOf(String) and substring(int, int).
The indexOf(String) function will allow you to find the first occurrence of a substring while substring(int, int) will extract a substring with the given indexes. Here's a short example of how to use them together:
Expand|Select|Wrap|Line Numbers
  1. String alphabet = "abcdefghijklmnopqrstuvwxyz";
  2. int beginIndex = alphabet.indexOf("fghi");
  3. System.out.println(alphabet.substring(beginIndex, beginIndex + 3));
The output of the above code is fgh.
This method can be applied to your problem easily.
Aug 23 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.