java字符串添加字符_如何在Java中向字符串添加字符?

java字符串添加字符

Case 1: In a simple way we will learn how to add characters by using predefined methods.

情况1:以简单的方式,我们将学习如何使用预定义的方法添加字符。

  • With the help of Java StringBuffer insert(int offset, String s) method.

    借助于Java StringBuffer insert(int offset,String s)方法 。

  • This method is available in package java.lang.StringBuffer.insert(int offset, String s).

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

  • This method is used to insert the string representation with the given object at the specified position.

    此方法用于在指定位置插入具有给定对象的字符串表示形式。

  • This method is overridable so it is available in different-2 forms like:

    此方法是可重写的,因此有两种不同的形式可用,例如:

    1. StringBuffer insert(int offset , boolean b)
    2. StringBuffer insert(int offset , char c)
    3. StringBuffer insert(int offset , char[] c) etc.
  • The return type of this method is StringBuffer that means this method returns a reference to this object.

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

  • We can pass two or three objects according to our need as a parameter in the method and that object will insert in a sequence at the specified position.

    我们可以根据需要传递两个或三个对象作为方法中的参数,并且该对象将按顺序插入到指定位置。

Example:

例:

import java.lang.StringBuffer;

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

        StringBuffer sb = new StringBuffer("Java is a programming language : ");

        // use insert(int offset ,Boolean b) 
        // it will insert the Boolean parameter at index 33 
        // as string to the StringBuffer
        sb.insert(33, true);

        // Display result after inserting
        System.out.println("The result will be after inserting Boolean object to the StringBuffer is :" + sb.toString());

        sb = new StringBuffer("Is java support OOPS : ");

        // use insert(int offset , int i) 
        // it will insert the String parameter at index 21 
        // as string to the StringBuffer
        sb.insert(21, "Yes");

        // Display result after inserting
        System.out.println("The result will be after inserting Integer object to the StringBuffer is :" + sb.toString());
    }
}

Output

输出量

D:\Programs>javac AddCharactersToString .java

D:\Programs>java AddCharactersToString
The result will be after inserting Boolean object to the StringBuffer is :Java is a programming language : true
The result will be after inserting Integer object to the StringBuffer is :Is Java Support OOPS : Yes

Case 2: How to add characters to a string in java with the help of logic?

情况2:如何借助逻辑在Java中的字符串中添加字符?

With the help of concatenating operator (+) we can add characters to a string. We will see in an example,

借助串联运算符(+),我们可以将字符添加到字符串中。 我们将在一个示例中看到

Example:

例:

public class AddCharactersToString {

    public static void main(String[] args) {
        String old_string = "Hello , Welcome in Java Worl";
        char added_char = 'd';
        String added_string = "d in version 8";

        old_string = old_string + added_char;
        String old_string1 = old_string + added_string;

        System.out.println(old_string);
        System.out.println(old_string1);
    }
    
}

Output

输出量

Output:
D:\Programs>javac AddCharactersToString.java

D:\Programs>java AddCharactersToString
Hello , Welcome in Java World
Hello , Welcome in Java Worldd in version 8


翻译自: https://www.includehelp.com/java/how-to-add-characters-to-a-string-in-java.aspx

java字符串添加字符

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值