Java tutorial
//package com.java2s; public class Main { public static String extractSubstring(String string, int n) { int index = string.indexOf("|"); while (n > 0) { if (index == -1) return (n == 0 ? string : null); n--; string = string.substring(index + 1); index = string.indexOf("|"); } return (index == -1 ? string : string.substring(0, index)); } }