How to remove empty lines from a formatted string

If you also want to remove lines that only contain whitespace, use resultString = Regex.Replace(subjectString, @”^\s+$[\r\n]*”, string.Empty, RegexOptions.Multiline); ^\s+$ will remove everything from the first blank line to the last (in a contiguous block of empty lines), including lines that only contain tabs or spaces. [\r\n]* will then remove the last CRLF (or just LF … Read more

Why StringJoiner when we already have StringBuilder?

StringJoiner is very useful, when you need to join Strings in a Stream. As an example, if you have to following List of Strings: final List<String> strings = Arrays.asList(“Foo”, “Bar”, “Baz”); It is much more simpler to use final String collectJoin = strings.stream().collect(Collectors.joining(“, “)); as it would be with a StringBuilder: final String collectBuilder = … Read more

Is .NET’s StringBuilder thread-safe

Absolutely not; here’s a simple example lifted from 4.0 via reflector: [SecuritySafeCritical] public StringBuilder Append(char value) { if (this.m_ChunkLength < this.m_ChunkChars.Length) { this.m_ChunkChars[this.m_ChunkLength++] = value; } else { this.Append(value, 1); } return this; } The attribute just handles callers, not thread-safety; this is absolutely not thread-safe. Update: looking at the source he references, this is … Read more

Most efficient solution for reading CLOB to String, and String to CLOB in Java?

Ok I will suppose a general use, first you have to download apache commons, there you will find an utility class named IOUtils which has a method named copy(); Now the solution is: get the input stream of your CLOB object using getAsciiStream() and pass it to the copy() method. InputStream in = clobObject.getAsciiStream(); StringWriter … Read more

Remove last character of a StringBuilder?

Others have pointed out the deleteCharAt method, but here’s another alternative approach: String prefix = “”; for (String serverId : serverIds) { sb.append(prefix); prefix = “,”; sb.append(serverId); } Alternatively, use the Joiner class from Guava 🙂 As of Java 8, StringJoiner is part of the standard JRE.