Skip to content

Commit

Permalink
added some more TextUtil quoting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Jan 28, 2024
1 parent ea43b3d commit e31575a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ the parameter.
## Changes
### 12.0.6
- TextUtil.quote(), TextUtil.quoteIfNeeded(), TextUtil.joinQuoted()
- use Java compatible quoting in Arguments.toString()
### 12.0.5
- add Arguments.toString()
Expand Down
42 changes: 39 additions & 3 deletions utility/src/main/java/com/dua3/utility/text/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,51 @@ public static String quoteIfNeeded(String text) {
}

/**
* Joins the elements in the given list into a single string, quoting each element. null elements are treated in the same way
* as empty strings.
* Joins the elements of the given list into a single string, using the specified delimiter.
* If an element needs to be quoted, it will be enclosed in quotes before joining.
*
* @param args the list of elements to be joined
* @param delimiter the delimiter to be used between the elements
* @return the joined string with each element quoted if necessary
*/
public static String joinQuotedIfNeeded(List<? extends Object> args, String delimiter) {
return args.stream().map(arg -> TextUtil.quoteIfNeeded(arg != null ? arg.toString() : "")).collect(Collectors.joining(delimiter));
}

/**
* Joins the elements of the given list into a single string, using ', ' as delimiter.
* If an element needs to be quoted, it will be enclosed in quotes before joining.
*
* @param args the list of elements to be joined
* @return the joined string with each element quoted if necessary
*/
public static String joinQuotedIfNeeded(List<? extends Object> args) {
return joinQuotedIfNeeded(args, ", ");
}

/**
* Joins the elements in the given list into a single string, quoting each element.
* Null elements are treated in the same way as empty strings.
*
* @param args The list of elements to be joined, can contain elements of any type.
* @param delimiter the delimiter to insert between args
* @return A single string that is the result of joining all the elements, with each element quoted and separated
* by a comma and space.
*/
public static String joinQuoted(List<? extends Object> args, String delimiter) {
return args.stream().map(arg -> TextUtil.quote(arg != null ? arg.toString() : "")).collect(Collectors.joining(delimiter));
}

/**
* Joins the elements in the given list into a single string, quoting each element and using ', ' as delimiter.
* Null elements are treated in the same way as empty strings.
*
* @param args The list of elements to be joined, can contain elements of any type.
* @return A single string that is the result of joining all the elements, with each element quoted and separated
* by a comma and space.
*/
public static String joinQuoted(List<? extends Object> args) {
return args.stream().map(arg -> TextUtil.quote(arg != null ? arg.toString() : "")).collect(Collectors.joining(", "));
return joinQuoted(args, ", ");
}

/**
Expand Down

0 comments on commit e31575a

Please sign in to comment.