Android String Sub String Get replaceOnce(String source, String subject, String object)

Here you can find the source of replaceOnce(String source, String subject, String object)

Description

replace Once

Declaration

public static String replaceOnce(String source, String subject,
        String object) 

Method Source Code

//package com.java2s;

public class Main {

    public static String replaceOnce(String source, String subject,
            String object) {/*w  w w.  j  av a 2  s .  co m*/
        StringBuffer rtnStr = new StringBuffer();
        String preStr = "";
        String nextStr = source;
        if (source.indexOf(subject) >= 0) {
            preStr = source.substring(0, source.indexOf(subject));
            nextStr = source.substring(
                    source.indexOf(subject) + subject.length(),
                    source.length());
            rtnStr.append(preStr).append(object).append(nextStr);
            return rtnStr.toString();
        } else {
            return source;
        }
    }

    public static int indexOf(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return -1;
        }
        return str.indexOf(searchStr);
    }
}

Related

  1. substringAfter(String str, String separator)
  2. substringBetween(String str, String open, String close)
  3. subStringEndString(String sourceStr, String endString)
  4. hightLinghtString(String destStr, String subStr, int color)
  5. replace(String source, String subject, String object)
  6. substring(String str, int toCount)
  7. substring(String str, int len, String more)
  8. substringFromLast(final String str, final String separator)
  9. substringToLast(final String str, final String separator)