Skip to content
Snippets Groups Projects
Commit ac41080b authored by Nicolas.Rod's avatar Nicolas.Rod Committed by Mathieu.Vonlanthen
Browse files

feat: new StringTool.removeTrailing() method

parent 00b1d3ab
No related branches found
No related tags found
1 merge request!333feat: new StringTool.removeTrailing() method
......@@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
......@@ -460,6 +460,21 @@ public class StringTool {
return str.substring(0, pos);
}
/**
* @param str
* @param trailingStr
* @return The string without the given final part
*/
public static String removeTrailing(final String str, String trailingStr) {
if (StringTool.isNullOrEmpty(str)) {
return str;
}
if (str.endsWith(trailingStr)) {
return str.substring(0, str.lastIndexOf(trailingStr));
}
return str;
}
/**
* This constructor should not be called
*/
......
......@@ -9,12 +9,12 @@
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
......@@ -26,6 +26,7 @@ package ch.unige.solidify.test.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
......@@ -356,4 +357,15 @@ class StringTest {
assertEquals(4, StringTool.countSubstringOccurrences(sub + "Lorem " + sub + " ipsum dolor " + sub + "sit eiusmod" + sub, sub));
}
@Test
void removeTrailingString() {
String trailingDiv = "</div>";
assertEquals("Some text", StringTool.removeTrailing("Some text</div>", trailingDiv));
assertEquals("Some text</div>", StringTool.removeTrailing("Some text</div></div>", trailingDiv));
assertEquals("</div>Some text</div>", StringTool.removeTrailing("</div>Some text</div></div>", trailingDiv));
assertEquals("Some text</div> ", StringTool.removeTrailing("Some text</div> ", trailingDiv));
assertEquals("</div>Some text", StringTool.removeTrailing("</div>Some text", trailingDiv));
assertEquals(null, StringTool.removeTrailing(null, trailingDiv));
assertThrows(NullPointerException.class, () -> StringTool.removeTrailing("Some text", null));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment