Affects: 6.1.4
I'm curious why the truncated String using StringUtils of the Spring Framework library is appending " (truncated)..." at the end. This made the length of my truncated String incorrect.
Consider the logs below:
2024-04-01T01:49:51.290+08:00 INFO 20212 --- [ restartedMain] c.r.llm.demo.SpringAiApplication : Initial String: abcdefghij 2024-04-01T01:49:51.292+08:00 INFO 20212 --- [ restartedMain] c.r.llm.demo.SpringAiApplication : Length of Initial String: 10 2024-04-01T01:49:51.292+08:00 INFO 20212 --- [ restartedMain] c.r.llm.demo.SpringAiApplication : Threshold: 5 2024-04-01T01:49:51.293+08:00 INFO 20212 --- [ restartedMain] c.r.llm.demo.SpringAiApplication : Truncated string: abcde (truncated)... 2024-04-01T01:49:51.293+08:00 INFO 20212 --- [ restartedMain] c.r.llm.demo.SpringAiApplication : Length of truncated string: 20
Simple code to reproduce the issue:
@Bean
CommandLineRunner run() {
return args -> {
var threshold = 5;
var stringWith10Length = "abcdefghij";
log.info("Initial String: {}", stringWith10Length);
log.info("Length of Initial String: {}", stringWith10Length.length());
log.info("Threshold: {}", threshold);
var truncateString = StringUtils.truncate(stringWith10Length, threshold);
log.info("Truncated string: {}", truncateString);
var truncatedStringLength = truncateString.length();
log.info("Length of truncated string: {}", truncatedStringLength);
};
}
Comment From: bclozel
This is the documented behavior of this utility method. See https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StringUtils.html#truncate(java.lang.CharSequence,int)
If you are expecting a different behavior I would suggest implementing your own - such utility methods are meant to be reused in Framework itself but not necessarily generally useful for applications.