Is there a Java equivalent to Python's Easy String Splicing?

前端 未结 8 1904
轮回少年
轮回少年 2020-12-29 03:24

Ok, what I want to know is is there a way with Java to do what Python can do below...

string_sample = \"hello world\"

string_sample[:-1]
>>> \"hell         


        
8条回答
  •  有刺的猬
    2020-12-29 04:17

    I have a created a simple library for that, called JavaSlice, which gives a uniform way to access slices of String, List or arrays in Java, in a similar manner as Python.

    So your examples would be simply written as this:

        String sample = "hello world";
    
        System.out.println(slice(sample, 0, -1)); // "hello worl"
        System.out.println(slice(sample, -1)); // 'd'
        System.out.println(slice(sample, 3)); // 'l'
    

提交回复
热议问题