Top Utility String In JAVA Often Used For, you may need for your Projects

Ia Kurnia
5 min readDec 25, 2021
Photo by Luca Bravo on Unsplash

In work your project, your source code has been made, can be a problem such as a source code redundancy or most of the time you repeat your self while writing common util functions.

Here are some of the most useful utilities that you can use for your project.

  • Check Has Value: this function is used if you check the condition if the value is not empty. If you do make not this function, problems that often arise are Null Pointer. If you don’t want Null Pointer, you must create this function so that problem is solved. This source such as code bellow
String EMPTY_STRING = "";
// #1. check hasValue parameter String
public static boolean hasValue(String param1) {
return param1 != null && !EMPTY_STRING.equals(param1.trim());
}

// #2. check hasValue parameter Object
public static boolean hasValue(Object param1) {
return param1 != null && hasValue(param1.toString());
}
  • Convert Value is Null: If the value is null will convert to an Empty String. This condition is associated with condition check null pointer, so that if the value is null and continues in the process then problems that often arise are Null Pointer. This source such as code bellow.
String EMPTY_STRING = "";
public static
String convertNullToString(String param1) {
return param1 == null ? EMPTY_STRING : param1;
}
  • UpperCase and LowerCase First Character: This condition if you want only the first character to be lowercase or uppercase. This source such as code bellow
// #4. Value Firt Character be Upper Case
public static String upperCaseFirstCharacter(String param1) {
return hasValue(param1) ? param1.substring(0, 1).toUpperCase() + param1.substring(1) : param1;
}

// #5. Value Firt Character be Lower Case
public static String lowerCaseFirstCharacter(String param1) {
return hasValue(param1) ? param1.substring(0, 1).toLowerCase() + param1.substring(1) : param1;
}
  • Pre Pad and Post Pad Value Custom value with input parameter character: This condition often is you want to add to String Value what you want well it’s in front or back your value such as front: 123IaKurnia or example back: front: IaKurnia123. This source Pre Pad Value such as code bellow.
// #6. Pre Pad Value Customize value with parameter character
public static String prePad(String param1, char param2, int param3) {
if (param3 < param1.length()) {
return param1;
} else {
int var1 = param3 - param1.length();
StringBuilder var2 = new StringBuilder();

for (int var3 = 0; var3 < var1; ++var3) {
var2.append(param2);
}

var2.append(param1);
return var2.toString();
}
}
  • This source Post Pad Value such as code bellow.
// #7. post Pad Value Customize value with parameter character
public static String postPad(String param1, char param2, int param3) {
if (param3 < param1.length()) {
return param1;
} else {
int var1 = param3 - param1.length();
StringBuilder var2 = new StringBuilder();
var2.append(param1);
for (int var3 = 0; var3 < var1; ++var3) {
var2.append(param2);
}
return var2.toString();
}
}
  • Chop value: This condition, if you want to set a temporary variable but the value is very long, or you want to save a database but that value is too long, then you must create this function for split value what you want and length value can be custom such as what you want. This source code is as below.
// #8. chop String value in list in according with what yout want in parameter type String
public static List<String> chopString(String param1) {
ArrayList var1 = null;
if (hasValue(param1)) {
var1 = new ArrayList();
int var2;
for (var2 = 0; param1.length() - var2 > ConstansValue.SIZE_CHOP; var2 += ConstansValue.SIZE_CHOP) {
String var3 = param1.substring(var2, var2 + ConstansValue.SIZE_CHOP);
var1.add(var3);
}
var1.add(param1.substring(var2));
}
return var1;
}

If your split value is a type byte you can follow this source code below.

// #9. chop String value in list in according with what yout want in parameter type Byte
public static List<String> chopByte(byte[] param1) {
ArrayList var1 = null;
if (hasValue(param1) && param1.length > 0) {
var1 = new ArrayList();
byte[] var2;
int var3;
for (var3 = 0; param1.length - var3 > ConstansValue.SIZE_CHOP; var3 += ConstansValue.SIZE_CHOP) {
var2 = new byte[ConstansValue.SIZE_CHOP];
System.arraycopy(param1, var3, var2, ConstansValue.ZERO, ConstansValue.SIZE_CHOP);
var1.add(var2);
}
var2 = new byte[param1.length - var3];
System.arraycopy(param1, var3, var2, ConstansValue.ZERO, param1.length - var3);
var1.add(var2);
}
return var1;
}
  • Glue Value: This condition is opposite to chop value, where this is function is merged between the first value and the next value. This source code is as below.
// #10. glue String in parameter type list
public static String glueString(List<String> param0) {
StringBuilder var1 = new StringBuilder();
if (hasValue(param0)) {
Iterator var2 = param0.iterator();

while (var2.hasNext()) {
String var3 = (String) var2.next();
var1.append(var3);
}
}
return var1.toString();
}

If your split value return is a type byte you can follow this source code below.

// #11. glue String in parameter type byte
public static byte[] glueByte(List<byte[]> param0) {
byte[] var1 = null;
if (hasValue(param0)) {
Iterator var4 = param0.iterator();

while (var4.hasNext()) {
byte[] var5 = (byte[]) var4.next();
if (var1 == null) {
var1 = var5;
} else {
byte[] var3 = var1;
var1 = new byte[var1.length + var5.length];
System.arraycopy(var3, 0, var1, 0, var3.length);
System.arraycopy(var5, 0, var1, var3.length, var5.length);
}
}
}
return var1;
}
  • Generate Random String: If you get a value random string, you can use this function. This source code as-is below.
// #12. generate random String
public static String generate() {
// generate random
return UUID.randomUUID().toString();
}
  • Append Wildcard Value: If has value, that value append Wildcard well it’s in front or back that value. This source code as-is below.
// #13. If hasvalue, that value append Wildcard
public static String appendWildcard(Object var0) {
// Usualy User Query parameter
return hasValue(var0) ? ConstansValue.WILDCARD + var0.toString() + ConstansValue.WILDCARD : ConstansValue.WILDCARD;
}
  • Format Decimal Value: If your value is a type data string, but you want to display with Format Decimal Standardization, you can this function. This source code as-is below.
// #15. Format Decimal Standarization_
public static String formatDecimal(BigDecimal param0) {
DecimalFormat var1 = new DecimalFormat("###,###,##0.00");
return var1.format(param0);
}

Maybe that function is often used in several projects. maybe you may need it for your project. If you want more to the complexity in String Utils Common, you can compare it with String Utils default Spring framework, you can download the package default String Utils Common at org.springframework.util.

This article is enough here, hopefully, it is useful. If you want to get this source code, you can clone the source code at the link below
https://github.com/iakurnia/StringUtilsCommon.git
See you in the next chapters

About the Author

I love new technology and follow the advancements in the field.
We Can Do It If We Want To Learn and Try.
Feel free to connect with me on my LinkedIn account
https://www.linkedin.com/in/ia-kurnia-a72778114/
Github: https://github.com/iakurnia

--

--

Ia Kurnia

I love new technology and follow the advancements in the field. We Can Do It If We Want To Learn and Try.