Created
December 9, 2015 14:09
-
-
Save eMahtab/7a2516417e912e93571f to your computer and use it in GitHub Desktop.
Splitting a String without using String split() method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Split { | |
public static void main(String[] args) { | |
List<String>pieces=new ArrayList<String>(); | |
Scanner sc=new Scanner(System.in); | |
String input=sc.nextLine().trim(); | |
System.out.println("Input :"+input); | |
char ch='s'; | |
int startIndex=0; | |
int matchIndex=-999; | |
while((matchIndex=input.indexOf(ch))!=-1){ | |
pieces.add(input.substring(startIndex,matchIndex)); | |
System.out.print(input.substring(startIndex,matchIndex)); | |
input=input.substring(matchIndex+1); | |
System.out.print("Next String :"+input); | |
System.out.println(); | |
} | |
System.out.println(input); | |
pieces.add(input); | |
System.out.println("Pieces : "+pieces); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment