Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
org.apache.commons.lang3.text.StrSubstitutor Example
What is org.apache.commons.lang3.text.StrSubstitutor class? How to use org.apache.commons.lang3.text.StrSubstitutor class?
✍: FYIcenter.com
org.apache.commons.lang3.text.StrSubstitutor class is a Java class
offered in commons-lang3.jar that
substitutes variables within a string by variable values.
Here is a simple example of org.apache.commons.lang3.text.StrSubstitutor class:
// Copyright (c) 2016 FYIcenter.com
import java.util.HashMap;
import java.util.Properties;
import org.apache.commons.lang3.text.StrSubstitutor;
// Example of using the StrSubstitutor class
public class StrSubstitutorExample {
public static void main(String[] args) throws Exception {
// Create a StrSubstitutor with a lookup map
HashMap map = new HashMap();
map.put("animal", "quick brown fox");
map.put("target", "lazy dog");
StrSubstitutor sub = new StrSubstitutor(map);
// Substitute an English template
String temp = "The ${animal} jumped over the ${target}.";
String str = sub.replace(temp);
System.out.println(str);
// Substitute a French template
temp = "Le ${animal} par dessus le ${target}.";
str = sub.replace(temp);
System.out.println(str);
// Create a list of properties as the lookup map
Properties lookup = new Properties();
lookup.setProperty("name", "John");
lookup.setProperty("url", "jar.fyicenter.com");
// Substitute a template with the list of properties
temp = "Hi ${name}, welcome to ${url}!";
str = StrSubstitutor.replace(temp, lookup);
System.out.println(str);
}
}
You can compile and run the above example in a command window as shown below:
C:\fyicenter>\local\jdk-1.8.0\bin\javac -cp C:\local\commons-lang3-3.5\commons-lang3-3.5.jar StrSubstitutor.java C:\fyicenter>\local\jdk-1.8.0\bin\java -cp .;C:\local\commons-lang3-3.5\commons-lang3-3.5.jar StrSubstitutor The quick brown fox jumped over the lazy dog. Le quick brown fox par dessus le lazy dog. Hi John, welcome to jar.fyicenter.com!
⇒ org.apache.commons.lang3.time.StopWatch Example
⇐ org.apache.commons.lang3.text.StrBuilder Example
2016-11-24, ∼3044🔥, 0💬
Popular Posts:
JDK 17 jdk.internal.vm.ci.jmod is the JMOD file for JDK 17 Internal VM CI module. JDK 17 Internal VM...
kernel.jar is a component in iText Java library to provide low-level functionalities. iText Java lib...
layout.jar is a component in iText Java library to provide layout functionalities. iText Java librar...
How to download and install ojdbc5.jar for Oracle 11g R1? ojdbc5.jar for Oracle 11g R1 is a Java 5 J...
JDK 11 jrt-fs.jar is the JAR file for JDK 11 JRT-FS (Java RunTime - File System) defined in the "jdk...