• string的一些方法


    package test;
    
    import java.awt.List;
    import java.awt.image.AreaAveragingScaleFilter;
    import java.lang.reflect.Array;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.*;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    import java.io.BufferedInputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Reader;
    import java.io.Writer;
    import javax.security.auth.callback.LanguageCallback;
    import javax.swing.text.StyledEditorKit.BoldAction;
    
    public class Main {
    
    	public static void main(String[] args) throws Exception {
    
    		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    		
    		char [] a = {'a','b','c','d','e'};
    		String s = new String(a,2,2);//前面是下标,后边是长度
    		System.out.println(s);
    		byte [] c = {97,98,99,100};
    		String ssString = new String(c);
    		System.out.println(ssString);
    
    		String string = "aaaasdsfsffgfgdhg";
    		//返回想找的字母第一个出现的位置的函数参数可以传int或者引号里面一个字母
    		System.out.println(string.indexOf('f'));
    		//从指定位置开始查找
    		System.out.println(string.indexOf('a', 2));
    		//从指定位置开始查找字符串,没找到返回-1
    		System.out.println(string.indexOf("aa", 4));
    		//查找最后出现的位置
    		System.out.println(string.lastIndexOf('a'));
    		//相对应的也有字符串查找
    		
    		//截取字符串前闭后开
    		System.out.println(string.substring(1, 4));
    		
    		//根据正则表达式的规则我们截取字符串
    		String s = "张三,李四,王五";
    		
    		String [] a = s.split(",");
    		for(int i = 0; i < a.length; i ++)
    		{
    			System.out.println(a[i]);
    		}
    	
    		//将字符串转换成字符数组
    		char []chs = s.toCharArray();
    		for (int i = 0; i < chs.length; i++) {
    			System.out.println(chs[i]);
    		}
    		System.out.println(s.length());
    		
    		//将字符串转换成字节
    		byte [] by = s.getBytes();
    		for (int i = 0; i < by.length; i++) {
    			System.out.println(by[i]);
    		}
    		
    		//转换大小写
    		System.out.println("abc".toUpperCase());
    		System.out.println("AAA".toLowerCase());
    		
    		//替换字符或者字符串
    		System.out.println("abc".replace('a', 'c'));
    		System.out.println("abc".replace("ab", "cc"));
    		
    		//去掉字符串两边空格中间的保留
    		System.out.println("   aaaaafd    sfsd  ".trim());
    		
    		//string.valueOf直接转换字符串类型
    		
    		//判断字符串的内容是否一致可以忽略大小写
    		System.out.println("abc".equalsIgnoreCase("ABc"));
    		//判断字符串是否包含另外一个字符串我们可以用indexof
    		//也可以用下面的方法
    		System.out.println(s.contains("aa"));
    		//找开头包含字符串
    		//找结尾包含字符串
    		System.out.println(s.startsWith("ds"));
    		System.out.println(s.endsWith("王五"));
    		
    		//比较对象大小
    		System.out.println(s.compareTo("aa"));
    
    	}
    }
    

      

  • 相关阅读:
    VSCode的终端修改
    vim总结
    分布式的CAP理论
    MongoDB常用命令总结
    nginx负载均衡总结
    【重要】nginx location配置和rewrite写法
    Linux四剑客find/grep/awk/sed
    IDEA插件开发总结
    支付宝在线支付接口开发流程
    Centos7 网卡Device does not seem to be present解决办法
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11788636.html
Copyright © 2020-2023  润新知