상세 컨텐츠

본문 제목

Array and string - Introduction to String from Java to Go

Go/Leet Code

by Gopythor 2022. 3. 30. 22:19

본문

728x90
반응형

Compare Function

Java

// "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));
    }
}

Go

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))
}

Result

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

Immutable or Mutable

Java

// "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);
    }
}

Go

func main() {
	var s1 string = "Hello World"
	s1[5] = ","
	fmt.Println(s1)
}

https://go.dev/play/p/m0gtFlpfnLX

Result

# command-line-arguments
.\main.go:7:8: cannot assign to s1[5] (strings are immutable)

Go

func main() {
	buf := []rune("Hello World")
	buf[5] = ','
	s := string(buf)
	fmt.Println(s) // "Hello,World"
}

Result

Hello,World

https://go.dev/play/p/yWqccn-dPP0

Extra Operation

Java

// "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));
    }
}

Go

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])
}

Result

Hello World!
The position of first 'o' is:  4
The position of last 'o' is:  7
World

https://go.dev/play/p/8n6n40yZ83H

728x90
반응형

관련글 더보기

댓글 영역