Example usage for java.lang String regionMatches

List of usage examples for java.lang String regionMatches

Introduction

In this page you can find the example usage for java.lang String regionMatches.

Prototype

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 

Source Link

Document

Tests if two string regions are equal.

Usage

From source file:com.test.stringtest.StringUtils.java

/**
 * <p>Case in-sensitive find of the first index within a String
 * from the specified position.</p>
 *
 * <p>A <code>null</code> String will return <code>-1</code>.
 * A negative start position is treated as zero.
 * An empty ("") search String always matches.
 * A start position greater than the string length only matches
 * an empty search String.</p>/*from ww  w. jav  a  2 s.c  om*/
 *
 * <pre>
 * StringUtils.indexOfIgnoreCase(null, *, *)          = -1
 * StringUtils.indexOfIgnoreCase(*, null, *)          = -1
 * StringUtils.indexOfIgnoreCase("", "", 0)           = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
 * StringUtils.indexOfIgnoreCase("abc", "", 9)        = 3
 * </pre>
 *
 * @param str  the String to check, may be null
 * @param searchStr  the String to find, may be null
 * @param startPos  the start position, negative treated as zero
 * @return the first index of the search String,
 *  -1 if no match or <code>null</code> string input
 * @since 2.5
 */
public static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return -1;
    }
    if (startPos < 0) {
        startPos = 0;
    }
    int endLimit = (str.length() - searchStr.length()) + 1;
    if (startPos > endLimit) {
        return -1;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }
    for (int i = startPos; i < endLimit; i++) {
        if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return -1;
}

From source file:com.sonicle.webtop.mail.Service.java

public Message reply(MailAccount account, MimeMessage orig, boolean replyToAll, boolean fromSent)
        throws MessagingException {
    MimeMessage reply = new MimeMessage(account.getMailSession());
    /*/*from  w ww.jav  a 2  s  . c  om*/
     * Have to manipulate the raw Subject header so that we don't lose
     * any encoding information.  This is safe because "Re:" isn't
     * internationalized and (generally) isn't encoded.  If the entire
     * Subject header is encoded, prefixing it with "Re: " still leaves
     * a valid and correct encoded header.
     */
    String subject = orig.getHeader("Subject", null);
    if (subject != null) {
        if (!subject.regionMatches(true, 0, "Re: ", 0, 4)) {
            subject = "Re: " + subject;
        }
        reply.setHeader("Subject", subject);
    }
    Address a[] = null;
    if (!fromSent)
        a = orig.getReplyTo();
    else {
        Address ax[] = orig.getRecipients(RecipientType.TO);
        if (ax != null) {
            a = new Address[1];
            a[0] = ax[0];
        }
    }
    reply.setRecipients(Message.RecipientType.TO, a);
    if (replyToAll) {
        Vector v = new Vector();
        Session session = account.getMailSession();
        // add my own address to list
        InternetAddress me = InternetAddress.getLocalAddress(session);
        if (me != null) {
            v.addElement(me);
        }
        // add any alternate names I'm known by
        String alternates = null;
        if (session != null) {
            alternates = session.getProperty("mail.alternates");
        }
        if (alternates != null) {
            eliminateDuplicates(v, InternetAddress.parse(alternates, false));
        }
        // should we Cc all other original recipients?
        String replyallccStr = null;
        boolean replyallcc = false;
        if (session != null) {
            replyallcc = PropUtil.getBooleanSessionProperty(session, "mail.replyallcc", false);
        }
        // add the recipients from the To field so far
        eliminateDuplicates(v, a);
        a = orig.getRecipients(Message.RecipientType.TO);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            if (replyallcc) {
                reply.addRecipients(Message.RecipientType.CC, a);
            } else {
                reply.addRecipients(Message.RecipientType.TO, a);
            }
        }
        a = orig.getRecipients(Message.RecipientType.CC);
        a = eliminateDuplicates(v, a);
        if (a != null && a.length > 0) {
            reply.addRecipients(Message.RecipientType.CC, a);
        }
        // don't eliminate duplicate newsgroups
        a = orig.getRecipients(MimeMessage.RecipientType.NEWSGROUPS);
        if (a != null && a.length > 0) {
            reply.setRecipients(MimeMessage.RecipientType.NEWSGROUPS, a);
        }
    }

    String msgId = orig.getHeader("Message-Id", null);
    if (msgId != null) {
        reply.setHeader("In-Reply-To", msgId);
    }

    /*
     * Set the References header as described in RFC 2822:
     *
     * The "References:" field will contain the contents of the parent's
     * "References:" field (if any) followed by the contents of the parent's
     * "Message-ID:" field (if any).  If the parent message does not contain
     * a "References:" field but does have an "In-Reply-To:" field
     * containing a single message identifier, then the "References:" field
     * will contain the contents of the parent's "In-Reply-To:" field
     * followed by the contents of the parent's "Message-ID:" field (if
     * any).  If the parent has none of the "References:", "In-Reply-To:",
     * or "Message-ID:" fields, then the new message will have no
     * "References:" field.
     */
    String refs = orig.getHeader("References", " ");
    if (refs == null) {
        // XXX - should only use if it contains a single message identifier
        refs = orig.getHeader("In-Reply-To", " ");
    }
    if (msgId != null) {
        if (refs != null) {
            refs = MimeUtility.unfold(refs) + " " + msgId;
        } else {
            refs = msgId;
        }
    }
    if (refs != null) {
        reply.setHeader("References", MimeUtility.fold(12, refs));
    }

    //try {
    //    setFlags(answeredFlag, true);
    //} catch (MessagingException mex) {
    //    // ignore it
    //}
    return reply;
}

From source file:com.clark.func.Functions.java

/**
 * <p>//  w w  w .ja va2s .c o  m
 * Checks if String contains a search String irrespective of case, handling
 * <code>null</code>. Case-insensitivity is defined as by
 * {@link String#equalsIgnoreCase(String)}.
 * 
 * <p>
 * A <code>null</code> String will return <code>false</code>.
 * </p>
 * 
 * <pre>
 * contains(null, *) = false
 * contains(*, null) = false
 * contains("", "") = true
 * contains("abc", "") = true
 * contains("abc", "a") = true
 * contains("abc", "z") = false
 * contains("abc", "A") = true
 * contains("abc", "Z") = false
 * </pre>
 * 
 * @param str
 *            the String to check, may be null
 * @param searchStr
 *            the String to find, may be null
 * @return true if the String contains the search String irrespective of
 *         case or false if not or <code>null</code> string input
 */
public static boolean containsIgnoreCase(String str, String searchStr) {
    if (str == null || searchStr == null) {
        return false;
    }
    int len = searchStr.length();
    int max = str.length() - len;
    for (int i = 0; i <= max; i++) {
        if (str.regionMatches(true, i, searchStr, 0, len)) {
            return true;
        }
    }
    return false;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*  www  . j  av  a2 s .  co  m*/
 * Check if a String ends with a specified suffix (optionally case
 * insensitive).
 * </p>
 * 
 * @see java.lang.String#endsWith(String)
 * @param str
 *            the String to check, may be null
 * @param suffix
 *            the suffix to find, may be null
 * @param ignoreCase
 *            inidicates whether the compare should ignore case (case
 *            insensitive) or not.
 * @return <code>true</code> if the String starts with the prefix or both
 *         <code>null</code>
 */
private static boolean endsWith(String str, String suffix, boolean ignoreCase) {
    if (str == null || suffix == null) {
        return str == null && suffix == null;
    }
    if (suffix.length() > str.length()) {
        return false;
    }
    int strOffset = str.length() - suffix.length();
    return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from  w w  w  . j a va2 s.  c  o m*/
 * Case in-sensitive find of the last index within a String from the
 * specified position.
 * </p>
 * 
 * <p>
 * A <code>null</code> String will return <code>-1</code>. A negative start
 * position returns <code>-1</code>. An empty ("") search String always
 * matches unless the start position is negative. A start position greater
 * than the string length searches the whole string.
 * </p>
 * 
 * <pre>
 * lastIndexOfIgnoreCase(null, *, *)          = -1
 * lastIndexOfIgnoreCase(*, null, *)          = -1
 * lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
 * lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
 * lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
 * lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
 * lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
 * lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
 * </pre>
 * 
 * @param str
 *            the String to check, may be null
 * @param searchStr
 *            the String to find, may be null
 * @param startPos
 *            the start position
 * @return the first index of the search String, -1 if no match or
 *         <code>null</code> string input
 * @since 2.5
 */
public static int lastIndexOfIgnoreCase(String str, String searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos > (str.length() - searchStr.length())) {
        startPos = str.length() - searchStr.length();
    }
    if (startPos < 0) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }

    for (int i = startPos; i >= 0; i--) {
        if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from  ww w. ja  v a2 s .com*/
 * Case in-sensitive find of the first index within a String from the
 * specified position.
 * </p>
 * 
 * <p>
 * A <code>null</code> String will return <code>-1</code>. A negative start
 * position is treated as zero. An empty ("") search String always matches.
 * A start position greater than the string length only matches an empty
 * search String.
 * </p>
 * 
 * <pre>
 * indexOfIgnoreCase(null, *, *)          = -1
 * indexOfIgnoreCase(*, null, *)          = -1
 * indexOfIgnoreCase("", "", 0)           = 0
 * indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
 * indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
 * indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
 * indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
 * indexOfIgnoreCase("aabaabaa", "B", -1) = 2
 * indexOfIgnoreCase("aabaabaa", "", 2)   = 2
 * indexOfIgnoreCase("abc", "", 9)        = 3
 * </pre>
 * 
 * @param str
 *            the String to check, may be null
 * @param searchStr
 *            the String to find, may be null
 * @param startPos
 *            the start position, negative treated as zero
 * @return the first index of the search String, -1 if no match or
 *         <code>null</code> string input
 * @since 2.5
 */
public static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos < 0) {
        startPos = 0;
    }
    int endLimit = (str.length() - searchStr.length()) + 1;
    if (startPos > endLimit) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }
    for (int i = startPos; i < endLimit; i++) {
        if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}