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));
    }//from   w w  w. jav  a 2  s  .  c  o m

    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