Posted In: Java Core, String

How to replace string inside curly brackets

Three possible ways 1. Use replace function. In this case do not escape curly brackets 2. Use replaceAll function. In this case escape curly brackets because replaceAll uses regular expression 3. Use java.text.MessageFormat class if string to replace is like {0}, {1}, {2} This code snippets were useful while testing Spring Rest URLs with a […]

Read More

Tags:

by , on October 21st, 2018

Posted In: Java Core, String

How to parse a mathematical expression given as a string – using ScriptEngine

Evaluating a math expression given in string form package com.example.mathexpression; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import org.junit.Test; public class MathExpression2 { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String result = “5+4*(7-15)”; System.out.println(engine.eval(result)); } }

Read More

Tags: Tags:

by , on August 30th, 2017

Posted In: Java Core, String

How to parse a mathematical expression given as a string – using Bean Shell

Evaluating a math expression given in string form Maven beanshell beanshell import package com.example.mathexpression; import org.junit.Test; import bsh.Interpreter; public class MathExpression { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); Interpreter interpreter = new Interpreter(); interpreter.eval(“result = 5+4*(7-15)”); System.out.println(interpreter.get(“result”)); } }

Read More

Tags: Tags:

by , on August 30th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Guava Splitter

Splitting a comma-separated string but ignoring commas in quotes Maven guava Guava Splitter Works only without commas in quotes data Works with commas in quotes data

Read More

Tags: Tags:

by , on August 29th, 2017

Posted In: Java Core, String

How to read and parse CSV file using String Pattern

Splitting a comma-separated string but ignoring commas in quotes Parsing CSV input with a RegEx in java

Read More

Tags: Tags:

by , on August 29th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Open CSV

Splitting a comma-separated string but ignoring commas in quotes Maven opencsv Example csv (cm24AUG2017bhav.csv) Example csv commas in quotes (cm24AUG2017bhav1.csv) opencsv import With deprecated CSVReader. Use CSVReaderBuilder With deprecated CSVReader. Use CSVReaderBuilder. commas in quotes data With CSVReaderBuilder With CSVReaderBuilder. commas in quotes data With CsvToBean and ColumnPositionMappingStrategy. CsvToBean.parse is deprecated With header element name

Read More

Tags: Tags:

by , on August 29th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Apache Commons CSV

Splitting a comma-separated string but ignoring commas in quotes Maven commons-csv Example csv Example csv commons csv import With element index With header element name With header name and with commas in quotes With header name RFC4180 format

Read More

Tags: Tags:

by , on August 29th, 2017

Posted In: Java Core, String

String vs StringBuilder vs StringBuffer Simple Benchmark test

Since String is immutable in java any changes to it creates a new object. StringBuffer and StringBuilder are mutable objects. They provide methods like append(), insert(), delete() and substring() to manipulate date.   This test clearly shows that always use StringBuilder. It will perform much better than String concatenation. Result

Read More

Tags:

by , on August 24th, 2017

Posted In: Java Core, String

When to use StringBuilder vs String concatenation

Most of the discussions say that for few fields String concatenation is fine. If code is looping then do use StringBuilder. Use StringBuilder for following kind of code There is slight different issue with this may be approach. Applications which go through various static code analysis tools should always use StringBuilder append than String concatenation. […]

Read More

Tags:

by , on August 24th, 2017

Posted In: Java Core, String

How to convert a String to an int in Java

Techniques used 1. Using Integer Class 2. Using BigInteger Class 3. Apache Commons IOUtils 4. Guava library  

Read More

Tags:

by , on August 22nd, 2017