Get the App
SLTechnology News&Howtos  ›  Development  › 

How java swaps two strings without using the third variable

Shulou Source: shulou.com Published: 2022-06-01 05:14:44 09月22日 Update

This article is about how java swaps two strings without using a third variable. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

How to swap two strings without a third variable

That's interesting, isn't it? In particular, the premise is that the third variable is not used.

public class SwapTwoStrings { public static void main(String[] args) { String s1 = "silence"; String s2 = "King II"; s1 = s1.concat(s2); s2 = s1.substring(0,s1.length()-s2.length()); s1 = s1.substring(s2.length()); System.out.println(s1); System.out.println(s2); } }

The output is as follows:

Wang Er was silent.

Tell me about my thoughts:

1)Concatenate two strings together using concat() method.

2)Then the substring() method takes the second string and the first string, respectively.

Thank you for reading! About "java how to exchange two strings without the third variable" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Tags: Character string three two variable no situation content method more article good practical premise idea meaning article condition see knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn Apple Huawei Shulou Information