Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static String stringByReplacingString(String source, String search, String replace, boolean replaceAll) {
        /**
        return ( null == source || null == search || 0 == search.length() ) ? source : replaceAll ?
           source.replaceAll( search , null == replace ) ? "" : replace ) :
           source.replaceFirst( search , null == replace ) ? "" : replace );
        /*/
        if (source == null)
            return null;
        if (search == null || search.length() == 0)
            return source;
        if (replace == null)
            replace = "";

        String string = "";
        int i, j, n, o;

        n = source.length();
        o = search.length();

        for (i = 0; i < n;) {
            if ((j = source.indexOf(search, i)) < 0)
                break;

            string = string.concat(source.substring(i, j));
            string = string.concat(replace);
            i += j + o;

            if (!replaceAll)
                break;
        }

        if (i < n)
            string = string.concat(source.substring(i));

        return string;
        /**/
    }
}