Add method to stringutil

This commit is contained in:
jamesagnew 2020-10-27 11:29:16 -04:00
parent 783cbb3cf8
commit 53c23b8d9b
3 changed files with 23 additions and 4 deletions

View File

@ -27,6 +27,17 @@ import java.util.Arrays;
public class StringUtil { public class StringUtil {
/**
* If a string ends with a given character, remove that character from the end of the string (as many times as it occurs at the end)
*/
public static String chompCharacter(String theInput, char theCharacter) {
String retVal = theInput;
while (retVal != null && retVal.length() > 0 && retVal.charAt(retVal.length() - 1) == theCharacter) {
retVal = retVal.substring(0, retVal.length() - 1);
}
return retVal;
}
public static String normalizeStringForSearchIndexing(String theString) { public static String normalizeStringForSearchIndexing(String theString) {
if (theString == null) { if (theString == null) {
return null; return null;
@ -57,7 +68,6 @@ public class StringUtil {
return new String(outBuffer.toCharArray()).toUpperCase(); return new String(outBuffer.toCharArray()).toUpperCase();
} }
public static String toUtf8String(byte[] theBytes) { public static String toUtf8String(byte[] theBytes) {
byte[] bytes = theBytes; byte[] bytes = theBytes;
if (theBytes.length >= 3) { if (theBytes.length >= 3) {

View File

@ -1,6 +1,5 @@
package ca.uhn.fhir.jpa.model.util; package ca.uhn.fhir.util;
import ca.uhn.fhir.util.StringUtil;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -42,4 +41,14 @@ public class StringUtilTest {
} }
@Test
public void testChompCharacter() {
assertEquals(null, StringUtil.chompCharacter(null, '/'));
assertEquals("", StringUtil.chompCharacter("", '/'));
assertEquals("", StringUtil.chompCharacter("/", '/'));
assertEquals("a", StringUtil.chompCharacter("a/", '/'));
assertEquals("a/a", StringUtil.chompCharacter("a/a/", '/'));
assertEquals("a/a", StringUtil.chompCharacter("a/a////", '/'));
}
} }

View File

@ -117,7 +117,7 @@ The `$partition-management-update-partition` operation can be used to update an
An HTTP POST to the following URL would be used to invoke this operation: An HTTP POST to the following URL would be used to invoke this operation:
```url ```url
http://example.com/$partition-management-create-partition http://example.com/$partition-management-update-partition
``` ```
The following request body could be used: The following request body could be used: