tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Template Engines > Freemarker > Split the string and get substrings by index

Split the string and get substrings by index 

FreeMarker is a java based template engine for complex template processing. This requires the library freemarker-2.3.16.jar to be in classpath. The following example shows using String split built-in function. We can reference the substrings by its index from the returned list.

File Name  :  
/FREEMARKER001/config/split2.ftl 

File Name  :  
com/bethecoder/tutorials/freemarker/tests/StringSplitTest2.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.freemarker.tests;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class StringSplitTest2 {

  /**
   @param args
   @throws IOException 
   @throws TemplateException 
   */
  public static void main(String[] argsthrows IOException, TemplateException  {

    //Get template from classpath
    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(StringSplitTest2.class, "/");
    Template template = cfg.getTemplate("split2.ftl");
    
    //Prepare data model
    Map<String, Object> dataModel = new HashMap<String, Object>();
    
    //Merge template and data
    OutputStreamWriter output = new OutputStreamWriter(System.out);
    template.process(dataModel, output);
  }
}
   

It gives the following output,

str1 = ONE 
str2 = TWO
str3 = THREE
str4 = FOUR


                    Print

   



Books Recommendation

 
 
bl  br