// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
// initialize
String s1 = "Hello World";
System.out.println("s1 is \"" + s1 + "\"");
String s2 = s1;
System.out.println("s2 is another reference to s1.");
String s3 = new String(s1);
System.out.println("s3 is a copy of s1.");
// compare using '=='
System.out.println("Compared by '==':");
// true since string is immutable and s1 is binded to "Hello World"
System.out.println("s1 and \"Hello World\": " + (s1 == "Hello World"));
// true since s1 and s2 is the reference of the same object
System.out.println("s1 and s2: " + (s1 == s2));
// false since s3 is refered to another new object
System.out.println("s1 and s3: " + (s1 == s3));
// compare using 'equals'
System.out.println("Compared by 'equals':");
System.out.println("s1 and \"Hello World\": " + s1.equals("Hello World"));
System.out.println("s1 and s2: " + s1.equals(s2));
System.out.println("s1 and s3: " + s1.equals(s3));
// compare using 'compareTo'
System.out.println("Compared by 'compareTo':");
System.out.println("s1 and \"Hello World\": " + (s1.compareTo("Hello World") == 0));
System.out.println("s1 and s2: " + (s1.compareTo(s2) == 0));
System.out.println("s1 and s3: " + (s1.compareTo(s3) == 0));
}
}
func main() {
// initialize
var s1 string = "Hello World"
fmt.Println("s1 is \"" + s1 + "\"")
var s2 *string = &s1
fmt.Println("s2 is another reference to s1")
var s3 string = s1
fmt.Println("s3 is a copy of s1")
// compare using '=='
fmt.Println("Compared by '==':")
// true since string is immutable and s1 is binded to "Hello World"
fmt.Println("s1 and Hello World:", (s1 == "Hello World"))
// true since s1 and s2 is the reference of the same object
fmt.Println("s1 and s2", (s1 == *s2))
// true since s1 and s3 is the copy of the object.
fmt.Println("s1 and s3", (s1 == s3))
// Golang doesn't need to use function for checking strings.
// compare returns when it is equal.
fmt.Println("Compared by 'Compare':")
fmt.Println("s1 and Hello World:", strings.Compare(s1, "Hello World"))
fmt.Println("s1 and s2", strings.Compare(s1, *s2))
fmt.Println("s1 and s3", strings.Compare(s1, s3))
}
s1 is "Hello World"
s2 is another reference to s1
s3 is a copy of s1
Compared by '==':
s1 and Hello World: true
s1 and s2 true
s1 and s3 true
Compared by 'Compare':
s1 and Hello World: 0
s1 and s2 0
s1 and s3 0
https://go.dev/play/p/uFJqZa57iBk
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
String s1 = "Hello World";
s1[5] = ',';
System.out.println(s1);
}
}
func main() {
var s1 string = "Hello World"
s1[5] = ","
fmt.Println(s1)
}
https://go.dev/play/p/m0gtFlpfnLX
# command-line-arguments
.\main.go:7:8: cannot assign to s1[5] (strings are immutable)
func main() {
buf := []rune("Hello World")
buf[5] = ','
s := string(buf)
fmt.Println(s) // "Hello,World"
}
Hello,World
https://go.dev/play/p/yWqccn-dPP0
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
String s1 = "Hello World";
// 1. concatenate
s1 += "!";
System.out.println(s1);
// 2. find
System.out.println("The position of first 'o' is: " + s1.indexOf('o'));
System.out.println("The position of last 'o' is: " + s1.lastIndexOf('o'));
// 3. get substring
System.out.println(s1.substring(6, 11));
}
}
func main() {
var s1 string = "Hello World"
// 1. concatenate
s1 += "!"
fmt.Println(s1)
// 2. find
fmt.Println("The position of first 'o' is: ", strings.Index(s1, "o"))
fmt.Println("The position of last 'o' is: ", strings.LastIndex(s1, "o"))
// 3. get substring
fmt.Println(s1[6:11])
}
Hello World!
The position of first 'o' is: 4
The position of last 'o' is: 7
World
[Go] Array and string - Add binary (0) | 2022.04.03 |
---|---|
[Go] Array and string - Immutable String - Problems & Solutions (0) | 2022.04.02 |
[Go] Array and string - Pascal's Triangle (0) | 2022.03.29 |
[Go] Array and String - Spiral Matrix (0) | 2022.03.29 |
[Go] Array and string - Diagonal Traverse (1) | 2022.03.27 |
댓글 영역