首页 > 技术文章 > Java字符串操作(连续更新)

OverloadBachPunk 2021-01-31 14:28 原文

Java字符串操作以及样例

样例来自leetcode

整数反转

  1. Integer.toString(int x):数值转字符串(类似的:Long.toString)
  2. str.substring(int x, int y):取子串,从x位开始,y位结束
  3. StringBuilder:提供一个可变的字符串,需要对字符串进行修改时,使用这个类
  4. stringbuilder.reverse():翻转字符串
  5. Stringbuilder.toString():转换回字符串
  6. Integer.valueOf(string str):字符串转回数值
class Solution {
    public int reverse(int x) {
        String str = Integer.toString(x);
        String string = str;
        int flag = 1;
        if (x < 0){
            flag = -1;
            string = str.substring(1);
        }
        try {
            return Integer.valueOf((new StringBuilder(string)).reverse().toString()) *flag;
        }catch (Exception e){
            return 0;
        }
    }
}

罗马数字转数值

  1. HashMap操作:初始化, 设置值,读取
  2. 读取字符串中某一位的字符:string.charAt(int x)

class Solution {
    public int romanToInt(String s) {
        Map<Character, Integer> dictMap = new HashMap<>();
        dictMap.put('I', 1);
        dictMap.put('V', 5);
        dictMap.put('X', 10);
        dictMap.put('L', 50);
        dictMap.put('C', 100);
        dictMap.put('D', 500);
        dictMap.put('M', 1000);
        int num1;
        int num2;
        int last = dictMap.get(s.charAt(s.length()-1));
        int output = 0;
        for (int i=0; i<s.length()-1; i++){
            num1 = dictMap.get(s.charAt(i));
            num2 = dictMap.get(s.charAt(i+1));
            if (num1 < num2){
                output -= num1;
            }
            else{
                output += num1;
            }
        }
        return output+=last;
    }
}

最长公共前缀

  1. 字符串组(数组)的长度返回:list.length(字符串的长度返回是方法)

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length==0) {
            return "";
        }
        String common_str = strs[0];
        for (int i=1; i<strs.length; i++){
            int j;
            for(j=0;j<common_str.length()&&j<strs[i].length();j++){
                if (common_str.charAt(j) != strs[i].charAt(j)){
                    break;
                }
            }
            if (j==0){
                return "";
            }
            //System.out.println(j);
            common_str = strs[i].substring(0, j);
        }
        return common_str;
    }
}

使用栈判断字符串是否符合格式

  1. 栈的声明:Stack stack = new Stack()
  2. 栈的push:stack.push()
  3. 栈非空:stack.isEmpty()
  4. 栈的pop: stack.pop()
  5. 字符串转字符列表:string.toCharArray()

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for(char c: s.toCharArray()){
            if(c=='('){
                stack.push(')');
            }
            else if(c=='['){
                stack.push(']');
            }
            else if(c=='{'){
                stack.push('}');
            }
            else if(stack.isEmpty()||c!=stack.pop()){
                return false;
            }
            System.out.println(stack);
        }
        return stack.isEmpty();
    }
}

外观数列

  1. StringBuilder可以像Python中的字符串那样进行操作:sb.append(char);
  2. 找到字符串中的第n个字符:s.charAt(int)
class Solution {
    public String countAndSay(int n) {
        if (n==1){
            return "1";
        }
        StringBuilder sb = new StringBuilder();
        String s = countAndSay(n - 1);
        char num = s.charAt(0);
        int count = 1;
        for(int j=1; j<s.length(); j++) {
            if(s.charAt(j)==num){
                count += 1;
            }
            else{
                sb.append(count).append(num);
                num = s.charAt(j);
                count = 1;
            }
        }
        sb.append(count).append(num);
        //System.out.println(sb.toString());
        return sb.toString();
    }
}

推荐阅读