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

前端 未结 8 1936
轮回少年
轮回少年 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:23

    You could easily write such a method, remember that negative indices are subtracted from the length of the string to get the correct index.

    public String slice(String s, int start) {
       if (start < 0) start = s.length() + start; 
    
       return s.substring(start);
    }
    

提交回复
热议问题