fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. String n = "hello\nworld\nhow\nare\nyou?";
  12. String r = "hello\rworld\rhow\rare\ryou?";
  13. String rn = "hello\r\nworld\r\nhow\r\nare\r\nyou?";
  14. System.out.println(substring(n, 0, 2));
  15. System.out.println(substring(r, 0, 2));
  16. System.out.println(substring(rn, 0, 2));
  17. System.out.println(substring(n, 1, 3));
  18. System.out.println(substring(r, 1, 3));
  19. System.out.println(substring(rn, 1, 3));
  20. System.out.println(substring(n, 3, 0));
  21. System.out.println(substring(r, 3, 0));
  22. System.out.println(substring(rn, 3, 0));
  23. }
  24.  
  25. public static String substring(String text, int line, int character) {
  26. int pos = 0;
  27. char sep = 0;
  28. while (line > 0) {
  29. char c = text.charAt(pos++);
  30. if (c == '\n' || c == '\r') {
  31. if (sep == 0)
  32. sep = c;
  33. if (c == sep)
  34. line--;
  35. }
  36. }
  37. char c = text.charAt(pos);
  38. if (c != sep && (c == '\n' || c == '\r'))
  39. pos++;
  40. return text.substring(0, pos + character);
  41. }
  42.  
  43. }
Success #stdin #stdout 0.07s 52644KB
stdin
Standard input is empty
stdout
he
he
he
hello
wor
hello
wor
hello
wor
hello
world
how

hello
world
how
hello
world
how