Skip to content

Instantly share code, notes, and snippets.

@webserveis
Created March 28, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webserveis/347649098244386a4085d37dfa253197 to your computer and use it in GitHub Desktop.
Save webserveis/347649098244386a4085d37dfa253197 to your computer and use it in GitHub Desktop.

DOS/Windows:\r\n Unix: "\n" MacOS: "\r"

Separar el texto por lineas

Independientemente del caracters o conjunto de caracteres pra definier un salto de linea.

  String[] lines = str.split("\r\n|\r|\n");
  for (String line : lines) {
      System.out.println(">>>" + line);
  }
public class MyClass {
    public static void main(String args[]) {

        String windowsStr ="Aquí la primera linea\r\nsegunda linea";
        String linuxStr ="Aquí la primera linea\nsegunda linea";
        String macosStr ="Aquí la primera linea\rsegunda linea";
        
        
        String str = windowsStr;

        System.out.println("break line type" + getBreakLineType(str));
        
        String[] lines = str.split("\r\n|\r|\n");
        for (String line : lines) {
            System.out.println(">>>" + line);
        }
        

    }
    
    public static String setBreakLine(String str) {
        
        return "";
    }

    
    public static int getBreakLineType(String str) {

        if (str.indexOf("\r\n")>0) {
            return 1; //Dos/Windows
        } else if (str.indexOf("\n\r")>0) {
            return 2; //Maquina de escribir vieja
        } else if (str.indexOf("\n")>0) {
            return 3; //Linux
        } else if (str.indexOf("\r")>0) {
            return 4; //MacOS
        } else {
            return 0;
        }

    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment