Sample code for 30+ languages & platforms
Android™

Get a Mail-Merge Replacement String by Index

See more Email Object Examples

Demonstrates the Chilkat Email.GetReplaceString method, which returns the replacement string for the Nth previously-defined pattern/replacement pair (a mail-merge feature). The index is zero-based and corresponds to the same index used by GetReplacePattern. This example defines two pairs and prints each pattern with its replacement.

Background: Pairing GetReplacePattern (the token to find) with GetReplaceString (the text to substitute) at the same index lets you walk the full mail-merge table. Retrieving a replacement by its position is handy when enumerating all substitutions; to look one up by its pattern instead, use GetReplaceString2.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //  Demonstrates the GetReplaceString method, which returns the replacement string for the
    //  Nth previously-defined pattern/replacement pair (a mail-merge feature).  The index is
    //  zero-based and corresponds to the same index used by GetReplacePattern.

    CkEmail email = new CkEmail();
    email.put_Subject("Hello FIRST_NAME");
    email.put_Body("Dear FIRST_NAME, welcome to CITY.");

    email.SetReplacePattern("FIRST_NAME","John");
    email.SetReplacePattern("CITY","Denver");

    int n = email.get_NumReplacePatterns();
    int i;
    for (i = 0; i <= n - 1; i++) {
        Log.i(TAG, email.getReplacePattern(i) + " -> " + email.getReplaceString(i));
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}