Development

Java, Python 및 JavaScript에서 문자열을 자르는 방법.

sonpro 2023. 3. 10. 04:10
반응형

Cut Strings

How to Cut Strings in Java, Python, and JavaScript

Summary: Cutting strings is a common task in programming. This article will guide you on how to cut strings in Java, Python, and JavaScript. We will cover the basic syntax and functions for each language and provide sample code examples.

Java

In Java, you can cut a string using the substring() method. This method takes two arguments: the starting index and the ending index. The starting index is inclusive, and the ending index is exclusive. Here's an example:

String str = "Hello World"; String cutStr = str.substring(0, 5); System.out.println(cutStr); // Output: Hello 

In this example, we are cutting the string "Hello World" from index 0 to index 5 (exclusive), which results in the string "Hello".

You can also use the substring() method to cut a string from a specific index to the end of the string:

String str = "Hello World"; String cutStr = str.substring(6); System.out.println(cutStr); // Output: World 

In this example, we are cutting the string "Hello World" from index 6 to the end of the string, which results in the string "World".

Python

In Python, you can cut a string using slicing. Slicing is done using the colon (:) operator. The syntax for slicing is string[start:end], where start is the starting index and end is the ending index. Here's an example:

str = "Hello World" cutStr = str[0:5] print(cutStr) # Output: Hello 

In this example, we are cutting the string "Hello World" from index 0 to index 5 (exclusive), which results in the string "Hello".

You can also use slicing to cut a string from a specific index to the end of the string:

str = "Hello World" cutStr = str[6:] print(cutStr) # Output: World 

In this example, we are cutting the string "Hello World" from index 6 to the end of the string, which results in the string "World".

JavaScript

In JavaScript, you can cut a string using the substring() method. This method takes two arguments: the starting index and the ending index. The starting index is inclusive, and the ending index is exclusive. Here's an example:

let str = "Hello World"; let cutStr = str.substring(0, 5); console.log(cutStr); // Output: Hello 

In this example, we are cutting the string "Hello World" from index 0 to index 5 (exclusive), which results in the string "Hello".

You can also use the substring() method to cut a string from a specific index to the end of the string:

let str = "Hello World"; let cutStr = str.substring(6); console.log(cutStr); // Output: World 

In this example, we are cutting the string "Hello World" from index 6 to the end of the string, which results in the string "World".

Conclusion

Cutting strings is a common task in programming. In this article, we covered how to cut strings in Java, Python, and JavaScript. We learned that Java and JavaScript use the substring() method, while Python uses slicing. We also provided sample code examples for each language. With this knowledge, you should be able to cut strings in your own programs.

반응형