asp.net 字串操作符+和+=詳解

來源:互聯網
上載者:User

       java中+和+=操作符除了之前講到的作用外,在特殊的上下文環境中是可以用作字元串連的。所以他們又稱為字串操作符,字串操作符有一些很有趣的行為。如果運算式以一個字串起頭,那麼後續所有的運算元都必須是字串型(編譯器會自動將雙引號中的字元序列傳化成字串):

public class stringoperators{

       public static void main(string [] args){

              int x=0,y=1,z=2;

              string s=”x,y,z”;

              system.out.println(s+x+y+z);

              system.out.println(x+””+s);

              s+=”(summed) =”;

              system.out.println(s+(x+y+z));

              system.out.println(“”+x);

}

}

輸出

x,y,z 012

0 x,y,z

x,y,z (summed) = 3

0

       應注意的是第一行輸出的是012而不是總和3,是因為編譯器將他們自動轉化成了字串形式,最後部分用+=講一個字串追加到了s上,並且用括弧來控制了編譯器的轉換,使他們可以順利求和

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.