zoukankan      html  css  js  c++  java
  • JAVA操作字符串

    package com.test;
    
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     *JAVA字符串操作
     */
    public class StringTest {
    
        private static final Logger logger = LoggerFactory.getLogger(StringTest.class);
        /**
         * 删除最后一位字符串
         */
        @Test
        public void deleteStr(){
            String str = "123456789*";
            //用的最多的是Substring
            logger.debug(str.substring(0,str.length()-1));
        }
    
        /**
         * 截取指定字符串
         */
        @Test
        public void cutOutStr(){
            String str = "hello world";
            //截取hello
            logger.debug(str.substring(str.lastIndexOf("h"),str.lastIndexOf(" ")));
            //去掉第一位 
            logger.debug(str.substring(1));
            //截取指定位数
            logger.debug(str.substring(1,7));
        }
        /**
         *字符串替换
         */
        @Test
        public void operateStr(){
            String str = "123";
            logger.debug(str.replace("12","22"));
        }
      /**
       *字符串去掉
       */
      @Test
      public void RemoveStr(){
      String str = "Hello World "";
      str = str.replaceAll("\\","");
      System.out.println(str);
      }

      /**
      *字符长度
      *
      */
      @Test
      public  int getStrLength(String str){
      str = str.replaceAll("[^\x00-\xff]", "**");
           return str.length;
        } }
  • 相关阅读:
    python 迭代器
    python 装饰器
    python 函数进阶
    python 函数
    python文件操作
    python 集合 深浅拷贝
    python基础之循环
    python基础之字典
    python基础之操作列表
    python基础之列表
  • 原文地址:https://www.cnblogs.com/qinxu/p/8193448.html
Copyright © 2011-2022 走看看