๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[JAVA ์ฝ”๋”ฉ ์ดˆ๋ณด] String(์ŠคํŠธ๋ง) Method ๋ณธ๋ฌธ

๐Ÿฆฅ ์ฝ”ํ…Œ/JAVA

[JAVA ์ฝ”๋”ฉ ์ดˆ๋ณด] String(์ŠคํŠธ๋ง) Method

์ง•์ง•์•ŒํŒŒ์นด 2023. 7. 16. 00:29
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” codechacha ๋‹˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

๐Ÿ’— String Method

๐Ÿ‰ ๊ธธ์ด ๋ฐ˜ํ™˜ & ๋ฌธ์ž์—ด ์ฒดํฌ & ๋ฌธ์ž ์ฐพ๊ธฐ

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main
{
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args)
    {
        String str = "My name is gani";

        // ๊ธธ์ด ๋ฐ˜ํ™˜
        System.out.println(str.length());

        // ๋นˆ ๋ฌธ์ž์—ด ์ฒดํฌ
        System.out.println(str.isEmpty());

        // ๋ฌธ์ž ์ฐพ๊ธฐ
        System.out.println(str.charAt(0));             // 'M' -> ๋ฌธ์ž ๋ฐ˜ํ™˜
        System.out.println(str.indexOf("a"));          // 0 -> ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
        System.out.println(str.lastIndexOf("i"));      // i -> ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฌธ์ž๊ฐ€ ์†ํ•œ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜       
    }
}

 

๐Ÿ‰ ๋ฌธ์ž ์ž๋ฅด๊ธฐ & ๋ฌธ์ž ์น˜ํ™˜

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main {
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args) {
        String str = "My name is gani";

        // ๋ฌธ์ž ์ž๋ฅด๊ธฐ
        System.out.println(str.substring(1, 3)); // ์ธ๋ฑ์Šค 1 ์ด์ƒ 3 ๋ฏธ๋งŒ ์œ„์น˜์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
        System.out.println(str.substring(3)); // ์ธ๋ฑ์Šค 3 ๋ฏธ๋งŒ ์œ„์น˜์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

        // ๋ฌธ์ž ์น˜ํ™˜(๋ฐ”๊พธ๊ธฐ)
        // replace([๊ธฐ์กด๋ฌธ์ž], [๋ฐ”๊ฟ€๋ฌธ์ž])
        System.out.println(str.replace('g', 'n')); // ๋ชจ๋“  [๊ธฐ์กด ๋ฌธ์ž]๋ฅผ [๋ฐ”๊ฟ€ ๋ฌธ์ž]๋กœ ์น˜ํ™˜

        // replaceAll([์ •๊ทœ์‹], [๋ฐ”๊ฟ€๋ฌธ์ž])
        System.out.println(str.replaceAll(".", "/")); // "/////" -> ์ •๊ทœ์‹์— ๋งž์ถฐ ๋ฌธ์ž ์น˜ํ™˜ (์ •๊ทœ์‹ "." ์€ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์˜๋ฏธ)

        String str2 = "gani";
        // replaceFirst([๊ธฐ์กด๋ฌธ์ž], [๋ฐ”๊ฟ€๋ฌธ์ž])
        System.out.println(str2.replaceFirst('i', 'p')); // ์—ฌ๋Ÿฌ ๋ฌธ์ž ์ค‘ ์ฒซ๋ฒˆ์งธ๋งŒ ์น˜ํ™˜
    }
}

 

๐Ÿ‰ ๋ฌธ์ž ๋™์ผ ์—ฌ๋ถ€ ํŒ๋‹จ & ๋ฌธ์ž ๋น„๊ต & ๋ฌธ์ž ํฌํ•จ ์—ฌ๋ถ€ ํŒ๋‹จ & ๋ฌธ์ž์™€ ์ˆซ์ž ์น˜ํ™˜

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main {
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args) {
        String str = "apple";

        // ๋ฌธ์ž ๋™์ผ ์—ฌ๋ถ€ ํŒ๋‹จ
        // ์ž๋ฐ” string์˜ ๊ฒฝ์šฐ, ํด๋ž˜์Šค๋กœ์จ Call by Referenceํ˜•ํƒœ๋กœ ์ƒ์„ฑ ์‹œ ์ฃผ์†Œ๊ฐ’์ด ๋ถ€์—ฌ
        // => Stringํƒ€์ž…์„ ์„ ์–ธํ–ˆ์„๋•Œ๋Š” ๊ฐ™์€ ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋”๋ผ๋„ ์„œ๋กœ๊ฐ„์˜ ์ฃผ์†Œ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค.
        // => ๊ฐ’ ๋น„๊ต๋กœ๋Š” equals๋ฅผ ์‚ฌ์šฉ
        System.out.println(str.equals("apple"));

        //๋ฌธ์ž ๋น„๊ต
        /**
        * str๊ณผ ๊ฐ™์œผ๋ฉด 0
        * str์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์ด๋ฉด -1
        * str์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ๋’ค๋ฉด 1
        * str์ด  ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋งŒ ๋‹ค๋ฅด๋ฉด, ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์˜ ์‚ฌ์ „์ˆœ ์ฐจ์ด ๋ฐ˜ํ™˜
        */
        System.out.println(str.compareTo("applq"));

        //๋ฌธ์ž ํฌํ•จ ์—ฌ๋ถ€ ํŒ๋‹จ
        System.out.println(str.contains("app"));

        //๋ฌธ์ž <-> ์ˆซ์ž ๋ณ€ํ™˜
        System.out.println(Integer.parseInt("100"));    // ๋ฌธ์ž์—ด "100"์„ ์ˆซ์ž 100์œผ๋กœ ๋ณ€ํ™˜
        System.out.println(Integer.toString(100));      // ์ˆซ์ž 100์„ ๋ฌธ์ž์—ด "100"์œผ๋กœ ๋ณ€ํ™˜
    }
}

 

๐Ÿ‰ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ & ๋ฌธ์ž ์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ

import java.util.*;
import java.io.*;

// tip: each public class is put in its own file
public class main {
    // tip: arguments are passed via the field below this editor
    public static void main(String[] args) {
        String str = "apple";

        // ๋ฌธ์ž์—ด ๋ถ„๋ฆฌ
        System.out.println(str.split(" ")); // ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์ž์—ด str์„ ๋ถ„๋ฆฌํ•˜์—ฌ String[] ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
        System.out.println(str.split()); // ๋„์–ด์“ฐ๊ธฐ ์—†๋Š” ๋ฌธ์ž์—ด str์„ ํ•œ ๋ฌธ์ž์”ฉ ๋ถ„๋ฆฌํ•˜์—ฌ String[] ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

        // ๋ฌธ์ž ์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ
        System.out.println(str.trim()); // str์˜ ์•ž๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ๋‹ค. ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๊ณต๋ฐฑ์€ ์ œ๊ฑฐํ•˜์ง€ ์•Š๋Š”๋‹ค.
    }
}

 

728x90
๋ฐ˜์‘ํ˜•
Comments