How to extract two characters from a given string at the same time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How to extract two characters from a given string at the same time?

I want two characters to extract.

29th Feb 2020, 2:50 PM
shubham kumar
shubham kumar - avatar
9 Answers
+ 3
try with substring
29th Feb 2020, 2:52 PM
Oma Falk
Oma Falk - avatar
+ 2
Question is unclear for me.. Can you explain more with an example? "Also share your code what you tried.. charAt(i) will give you character at i'th index. So i+1 give you next char..
29th Feb 2020, 2:54 PM
Jayakrishna 🇮🇳
+ 1
if i have a string of "Hello, World!" which character you want to extract ?
29th Feb 2020, 2:52 PM
Taste
Taste - avatar
+ 1
try substring as Oma Falk and Jayakrishna said. use substring and combination of i+1. so fpr example i=0; s.substring(i,i+2) //its not +1 sorry //end index is exclusive now modify i so next substring wont overlap with previous substring
29th Feb 2020, 2:59 PM
Taste
Taste - avatar
+ 1
i=0; s.charAt(i)+""+s.charAt(i+1); Or else go use i=0; s.substring(i, i+2); i+=2;
29th Feb 2020, 3:04 PM
Jayakrishna 🇮🇳
0
if you don't like indexes import java.util.Scanner; .. String str = "12345678"; Scanner sc = new Scanner(str) .useDelimiter("(?<=\\G..)"); //(?<= look behind, \\G from last match, .. two chars while( sc.hasNext() ) { System.out.println( sc.next() ); }
1st Mar 2020, 3:19 AM
zemiak
- 1
Everytime you can't go for index finding to put it in substring function.
29th Feb 2020, 2:54 PM
shubham kumar
shubham kumar - avatar
- 1
I want first two characters to come,then next two characters and so on till the end.
29th Feb 2020, 2:56 PM
shubham kumar
shubham kumar - avatar
- 1
Not satisfied with your answer.
29th Feb 2020, 3:00 PM
shubham kumar
shubham kumar - avatar