You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2019/01/04 10:19:17 UTC

[GitHub] Yash-777 closed pull request #394: Replace String for reports

Yash-777 closed pull request #394: Replace String for reports
URL: https://github.com/apache/commons-lang/pull/394
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 7fcd0246e..0f5f0fc1d 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -3124,6 +3124,41 @@ public static String substringBetween(final String str, final String open, final
         return list.toArray(new String [list.size()]);
     }
 
+    /**
+     * <p>Replaces the given String, with the String which is nested in between two Strings.</p>
+     *
+     * <p>A {@code null} input String returns {@code null}.
+     * A {@code null} open/close returns {@code null} (no match).
+     * An empty ("") open and close returns an empty string.</p>
+     *
+     * @param str  the String containing the substring, may be null
+     * @param replace the Sting to be replaced, which is nested in between open and close substrings
+     * @param open  the String before the substring, may be null
+     * @param close  the String after the substring, may be null
+     * @return the substring, {@code null} if no match
+     */
+    public static String replaceSubstringInBetween(final String str, final String replace, final String open, final String close) {
+        if (str == null || open == null || close == null) {
+            return null;
+        }
+        final int start = str.indexOf(open);
+        if (start != INDEX_NOT_FOUND) {
+            String preceding = "";
+            if (start > 0) {
+                preceding = str.substring(0, start);
+            }
+            final int end = str.indexOf(close, start + open.length());
+            if (end != INDEX_NOT_FOUND) {
+                String exceding = "";
+                if ((end < str.length() - 1) && (end + close.length() < str.length())) {
+                    exceding = str.substring(end + close.length(), str.length());
+                }
+                //String middleString = str.substring(start + open.length(), end);
+                return preceding + open + replace + close + exceding;
+            }
+        }
+        return null;
+    }
     // Nested extraction
     //-----------------------------------------------------------------------
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services