Added new string methods.
authorMarco Zanon <info@marcozanon.com>
Thu, 25 Jul 2019 20:38:06 +0000 (20:38 +0000)
committerMarco Zanon <info@marcozanon.com>
Thu, 25 Jul 2019 20:38:06 +0000 (20:38 +0000)
src/java/com/marcozanon/macaco/database/MDatabaseConnection.java
src/java/com/marcozanon/macaco/text/MText.java

index 09df47f330e9e232e29efc20b30a43312cd97249..4ce8bcc2e31cd46a5d9335677ca544d190215c24 100644 (file)
@@ -186,6 +186,7 @@ public class MDatabaseConnection extends MObject {
         if (MDatabaseConnection.TransactionStatus.CLOSED != this.getTransactionStatus()) {
             throw new MSqlTransactionException("Nested transactions not allowed.");
         }
+        //
         try {
             // Check the connection.
             this.check();
@@ -207,6 +208,7 @@ public class MDatabaseConnection extends MObject {
         if (MDatabaseConnection.TransactionStatus.CLOSED == this.getTransactionStatus()) {
             throw new MSqlTransactionException("Transaction not started.");
         }
+        //
         try {
 /*
             // Check the connection.
index 1fef4ea18388eed17433fed2ab3f9f8683c47019..0af3ebb5fd5f64c29d86a786c57c77e2857d8dbc 100644 (file)
@@ -12,6 +12,7 @@ import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.util.StringJoiner;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
@@ -69,6 +70,43 @@ public class MText extends MObject {
         return y.toString();
     }
 
+    public static String removeLine(String x, int lineNumber) {
+        if (null == x) {
+            throw new IllegalArgumentException("Invalid 'x': null.");
+        }
+        //
+        String[] xLines = x.split(System.getProperty("line.separator"));
+        //
+        if ((0 > lineNumber) || (xLines.length - 1 < lineNumber)) {
+            throw new IllegalArgumentException(String.format("Invalid 'lineNumber': %s.", lineNumber));
+        }
+        //
+        StringJoiner stringJoiner = new StringJoiner(System.getProperty("line.separator"));
+        for (int l = 0; xLines.length > l; l++) {
+            if (l == lineNumber) {
+                continue;
+            }
+            //
+            stringJoiner.add(xLines[l]);
+        }
+        //
+        return stringJoiner.toString();
+    }
+
+    public static String removeFirstLine(String x) {
+        return MText.removeLine(x, 0);
+    }
+
+    public static String removeLastLine(String x) {
+        if (null == x) {
+            throw new IllegalArgumentException("Invalid 'x': null.");
+        }
+        //
+        String[] xLines = x.split(System.getProperty("line.separator"));
+        //
+        return MText.removeLine(x, xLines.length - 1);
+    }
+
     /* Javascript escape. */
 
     public static String getJavascriptEscapedString(String x) {