Java - Write code to replace First match

Requirements

Write code to replace First match

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String origin = "book2s.com";
        String match = "o";
        String replacement = "O";
        System.out.println(replaceFirst(origin, match, replacement));
    }/*  w  w  w  . ja  v a2  s  . c  om*/

    public static String replaceFirst(String origin, String match,
            String replacement) {
        StringBuffer sb = new StringBuffer(origin);
        int begin = origin.indexOf(match);
        if (begin != -1) {
            sb.delete(begin, begin + match.length());
            sb.insert(begin, replacement);
        }
        return sb.toString();
    }
}

Related Exercise