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:
此方法是可重写的,因此有两种不同的形式可用,例如:
- StringBuffer insert(int offset , boolean b)
- StringBuffer insert(int offset , char c)
- 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字符串添加字符