Java StringBuffer用示例替换(int spos,int epos,String s)方法

StringBuffer类replace(int spos,int epos,String s) (StringBuffer Class replace(int spos, int epos , String s))

  • This method is available in package java.lang.StringBuffer.replace(int spos , int epos , String s).

    软件包java.lang.StringBuffer.replace(int spos,int epos,String s)中提供了此方法。

  • This method is used to replace the characters in a substring with the specified string in the method and substring starts from spos(Starting position) and ends with epos(Ending position).

    此方法用于用方法中的指定字符串替换子字符串中的字符,并且子字符串从spos (起始位置)开始,以epos (结束位置)结束。

  • The procedure of this method is first to delete all the characters in a substring and then start inserting specified string from starting position(spos) defined in a method.

    此方法的过程是首先删除子字符串中的所有字符,然后从方法中定义的起始位置( spos )开始插入指定的字符串。

Syntax:

句法:

    StringBuffer replace(int spos , int epos , String s){
    }

Parameter(s):

参数:

We pass three one object in the method of the StringBuffer i.e. spos(starting position), epos(ending position) and String s. The substring is in between spos and epos and the specified string will be inserted in between epos and spos after removing all the characters in a substring.

我们在StringBuffer方法中传递了三个对象,即spos (开始位置), epos (结束位置)和String s 。 子字符串位于spos和epos之间,并且在删除子字符串中的所有字符后,将在epos和spos之间插入指定的字符串。

Return value:

返回值:

The return type of this method is StringBuffer that means this method returns a reference of this object.

该方法的返回类型为StringBuffer,这意味着该方法返回该对象的引用。

Java程序演示replace()方法的示例 (Java program to demonstrate example of replace() method)

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer(" welcome in java world ");

        // use replace(int spos , int epos , String s) 
        // it will replace all the characters between 
        // spos and epos with the given string in the method.

        // Display result after implementing replace 
        // (12,16,"C++")i.e. java substring will be 
        // replaced by the string C++
        System.out.println("The result will be after implementing method is :" + sb.replace(12, 16, "C++"));
    }
}

Output

输出量

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after implementing method is : welcome in C++ world


翻译自: https://www.includehelp.com/java/stringbuffer-replace-int-spos-int-epos-string-s-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值