- 1
How to extract two characters from a given string at the same time?
I want two characters to extract.
9 Réponses
+ 3
try with substring
+ 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..
+ 1
if i have a string of "Hello, World!"
which character you want to extract ?
+ 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
+ 1
i=0;
s.charAt(i)+""+s.charAt(i+1);
Or else go use
i=0;
s.substring(i, i+2);
i+=2;
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() );
}
- 1
Everytime you can't go for index finding to put it in substring function.
- 1
I want first two characters to come,then next two characters and so on till the end.
- 1
Not satisfied with your answer.