如何使字符串添加到字符串功能?

这个问题在这里已经有了答案:

  • 在Java中通过引用传递字符串? 13个答案
  • 如何在Java中连接两个字符串? 19个答案

  • 我想你想要这样的东西:

      public String addLine(String one, String two){
        return one+two;
    }
    

    注意,这会返回一个字符串,所以在main中做类似的事情:

    text = addLine(text, "line1");
    

    确保你创建它作为一种方法:


    public class Text {
    private String text = "Hello";
    public Text(){}
    public Text(String text){
        this.text = text;
    }
    public void setText(String text){
        this.text = text;
    }
    public void addLine(String lnToAdd){
        text += "n" +lnToAdd ;
    }
    public String getText(){
        return text;
    }
    

    }


    public class Main {
    
    public static void main(String[] args) {
    
        Text text = new Text("Hello");
        System.out.println(text.getText()); //Returns Hello
        System.out.println();
        text.addLine("Java");
        System.out.println(text.getText()); /*Returns Hello
                                                      Java*/
    }
    

    }

    链接地址: http://www.djcxy.com/p/20881.html

    上一篇: How to make fuction which add String into String?

    下一篇: Pointer with java and string variable