[GitHub] Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings.
Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings. URL: https://github.com/apache/commons-lang/pull/395 Replace String with the String which is nested in between two Strings. I have used this Method in order to generate dynamic HTML reports to send a mail. ```java public class Test { public static void main(String[] args) throws Exception { String data = new String(Files.readAllBytes(Paths.get("./dynamicReport.html"))); String rowSaperator = ""; String rowDataSaperator = "[~$List$~]"; List dynamicRecords = new ArrayList<>(); dynamicRecords.add( new String[]{ "1", "1" } ); dynamicRecords.add( new String[]{ "1", "1" } ); dynamicRecords.add( new String[]{ "2", "1" } ); dynamicRecords.add( new String[]{ "1", "2" } ); String finalTableData = dynamicRecordData(data, rowSaperator, rowDataSaperator, dynamicRecords); String targetFilePath = "D:/dynamicReport.txt"; try (PrintWriter out = new PrintWriter( targetFilePath )) { out.println( finalTableData ); } } public static String dynamicRecordData(String str, String rowSaperator, String rowDataSaperator, List dynamicRecords) throws Exception { if( str.length() <= ( (rowSaperator.length())*2 + rowDataSaperator.length()) ) { return str; } String substringBetween = StringUtils.substringBetween(str, rowSaperator, rowSaperator); if( substringBetween == null) { return str; } int countMatches = StringUtils.countMatches(substringBetween, rowDataSaperator); if( str.length() <= ( (rowSaperator.length())*2 + (rowDataSaperator.length())*countMatches ) ) { return str; } StringBuffer dynamicRowData = new StringBuffer(); String[] searchList = null; for (String[] dynamicRecord : dynamicRecords) { if( dynamicRecord.length == countMatches ) { if( searchList == null ) { searchList = new String[countMatches]; for (int i = 0; i < countMatches; i++) { searchList[i] = rowDataSaperator; } } String replaceEach = StringUtils.replaceEach(substringBetween, searchList, dynamicRecord); dynamicRowData.append(replaceEach); } else { throw new Exception("Records mismatch."); } } String finalTableData = StringUtils.replaceSubstringInBetween(str, dynamicRowData.toString(), rowSaperator, rowSaperator); return finalTableData; } } ``` Sample HTML Template. Where i my case there are to many dynamic tables with provided list data to replace. ```html Header1Header2 [~$List$~][~$List$~] ``` After code generated report. ```html Header1Header2 11 11 22 11 ``` Avoid previous pull requests [`393`, `394`](https://travis-ci.org/apache/commons-lang/pull_requests) 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings.
Yash-777 opened a new pull request #395: Replaces the given String, with the String which is nested in between two Strings. URL: https://github.com/apache/commons-lang/pull/395 Replace String with the String which is nested in between two Strings. I have used this Method in order to generate dynamic HTML reports to send a mail. ```java public class Test { public static void main(String[] args) throws Exception { String data = new String(Files.readAllBytes(Paths.get("./dynamicReport.html"))); String rowSaperator = ""; String rowDataSaperator = "[~$List$~]"; List dynamicRecords = new ArrayList<>(); dynamicRecords.add( new String[]{ "1", "1" } ); dynamicRecords.add( new String[]{ "1", "1" } ); dynamicRecords.add( new String[]{ "2", "1" } ); dynamicRecords.add( new String[]{ "1", "2" } ); String finalTableData = dynamicRecordData(data, rowSaperator, rowDataSaperator, dynamicRecords); String targetFilePath = "D:/dynamicReport.txt"; try (PrintWriter out = new PrintWriter( targetFilePath )) { out.println( finalTableData ); } } public static String dynamicRecordData(String str, String rowSaperator, String rowDataSaperator, List dynamicRecords) throws Exception { if( str.length() <= ( (rowSaperator.length())*2 + rowDataSaperator.length()) ) { return str; } String substringBetween = StringUtils.substringBetween(str, rowSaperator, rowSaperator); if( substringBetween == null) { return str; } int countMatches = StringUtils.countMatches(substringBetween, rowDataSaperator); if( str.length() <= ( (rowSaperator.length())*2 + (rowDataSaperator.length())*countMatches ) ) { return str; } StringBuffer dynamicRowData = new StringBuffer(); String[] searchList = null; for (String[] dynamicRecord : dynamicRecords) { if( dynamicRecord.length == countMatches ) { if( searchList == null ) { searchList = new String[countMatches]; for (int i = 0; i < countMatches; i++) { searchList[i] = rowDataSaperator; } } String replaceEach = StringUtils.replaceEach(substringBetween, searchList, dynamicRecord); dynamicRowData.append(replaceEach); } else { throw new Exception("Records mismatch."); } } String finalTableData = TabularData_from_List.replaceSubstringInBetween(str, dynamicRowData.toString(), rowSaperator, rowSaperator); return finalTableData; } } ``` Sample HTML Template. Where i my case there are to many dynamic tables with provided list data to replace. ```html Header1Header2 [~$List$~][~$List$~] ``` After code generated report. ```html Header1Header2 11 11 22 11 ``` Avoid previous pull requests `393`, `394` 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: us...@infra.apache.org With regards, Apache Git Services