Leetcode:旋转字符串,String相关用法

//查找字符串中下标为index的字符,返回一个字符:
System.out.println("student".charAt(0));  //输出:s

//按照字典顺序比较两个字符串大小:
System.out.println("abc".compareTo("abc"));  //输出:0

//判断前面的字符串是否包含后面的字字符串:
System.out.println("hello World".contains("hello"));  //输出:true

public String trim()
//去除字符串前后的空格。
public boolean startsWith(String prefix)
//判断当前字符串是否以prefix字符串开头。

public boolean endsWith(String suffix)
//判断当前字符串是否以suffix字符串开结尾。
public int indexOf(String str)
//返回某个子字符串在当前字符串中第一次出现的下标,没有就返回-1。

public int lastIndexOf(String str)
//返回某个子字符串在当前字符串中最后一次出现的下标,没有就返回-1。
public String replace(CharSequence target, CharSequence replacement)
//将当前字符串当中的所有target字符串换成replacement字符串。

replaceFirst(CharSequence target, CharSequence replacement)
//将当前字符串当中的第一个target字符串换成replacement字符串。
//将当前字符串以regex字符串隔开,隔开后的片段以String[]形式返回。
public String[] split(String regex)

String[] ymd = "2020-1-1".split("-");
for (String x: ymd) {
    System.out.print(x + "  ");  //输出:2020  1  1  
}

//在当前字符串中,从beginIndex开始截取,截取到endIndex的新字符串,返回新字符串。
public String substring(int beginIndex, int endIndex)

String str1 = "abcdefgh".substring(3, 6);  //输出def

例题:剑指offer 58.旋转字符串

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof

class Solution {
    public String reverseLeftWords(String s, int n) {
        
        String s1=s.substring(0,n);
        String s2=s.substring(n,s.length());
        return s2+s1;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值