Java常用工具——java字符串

一、String常用字符串

package com.imooc.string;

public class StringDemo {
    public static void main(String[] args) {
        //定义一个字符串“JAVA 编程 基础”
        String str="JAVA 编程 基础";
        //打印输出字符串的长度
        System.out.println("字符串的长度是:"+str.length());
        //取出字符‘程‘并输出
        System.out.println(str.charAt(6));
        //取出子串"编程 基础"并输出
        System.out.println(str.substring(5));
        //取出子串"编程"并输出
        System.out.println(str.substring(5, 7));
    }

}
package com.imooc.string;

public class StringDemo2 {
    public static void main(String[] args) {
        //定义一个字符串“JAVA 编程 基础”
        String str="JAVA 编程 基础";
        //查找字符‘A‘在字符串中第一次出现的位置
        System.out.println("字符‘A‘在字符串中第一次出现的位置:"+str.indexOf(‘A‘));
        //查找字符‘A‘在字符串中最后一次出现的位置
        System.out.println("字符‘A‘在字符串中最后一次出现的位置:"+str.lastIndexOf(‘A‘));
        //查找子串"编程"在字符串中第一次出现的位置
        System.out.println("子串\"编程\"在字符串中第一次出现的位置:"+str.indexOf("编程"));
        //查找子串"编程"在字符串中最后一次出现的位置
        System.out.println("子串\"编程\"在字符串中最后一次出现的位置:"+str.lastIndexOf("编程"));
        //在字符串inde值为8的位置开始,查找子串"编程"在字符串中第一次出现的位置
        System.out.println("字符串inde值为8的位置开始,查找子串\"编程\"在字符串中第一次出现的位置:"+str.indexOf("编程", 8));

    }

}
package com.imooc.string;

import java.io.UnsupportedEncodingException;

public class StringDemo3 {

    public static void main(String[] args) throws UnsupportedEncodingException {
        // 字符串和byte数组之间的相互的转换
        //定义一个字符串
        String str=new String("JAVA 编程 基础");
        //1、将字符串转换为byte数组,并打印输出
        byte[] arrs=str.getBytes("GBK");
        for(int i=0;i<arrs.length;i++) {
            System.out.print(arrs[i]+" ");
        }
        System.out.println();
        //2、将byte数组转换为字符串
        String str1=new String(arrs,"GBK");
        System.out.println(str1);
    }

}



原文地址:https://www.cnblogs.com/loveapple/p/11140098.html

时间: 2024-08-30 08:41:31

Java常用工具——java字符串的相关文章

【Mooc】1.3 java常用工具类--字符串

关于String String s1="hello"; String s2="hello"; String s3=new String("hello"); String s4="hello"+s1; String s5="hello"+s1; String s6=new String("hello"+s1); System.out.println("值相同,s1与S2同内存地址

Java常用工具——java集合

一.ArrayList package com.imooc.set; import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { // 用ArrayList存储编程语言的名称,并输出. //名称包括”Java”.”C”.”C++“.”Go”和”Swift” List list=new ArrayList(); lis

java 常用工具

System:类中的属性方法都是静态的.无法实例化 err:"标准"错误输出流 in:"标准"输入流 out:"标准"输出流 常见方法: long currentTimeMillis();获取当前时间的毫秒值 Properties    getProperties(); 获取系统属性 Properties集合中存储的都是String类型的键和值. Runtime:没有构造方法摘要,说明该类不可以创建对象. 方法又是非静态,说明该类提供静态返回该类

java常用工具类(java技术交流群57388149)

package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY =

Java常用工具类集合

数据库连接工具类 仅仅获得连接对象 ConnDB.java package com.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库连接工具类——仅仅获得连接对象 * */ public class ConnDB { private static Connection conn = null; private static final String DRIVER_NAME = "com.mysql

java常用工具方法2

/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LI

java常用工具类(三)—— Excel 操作工具

import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; i

java常用工具类(三)—— 文件读取的操作类

定义常用的文件类型 public class FileType { /** * 文件头类型 */ public static final String XML_FILE = "text/xml;charset=UTF-8"; public static final String PDF_FILE = "application/pdf"; public static final String PDG_FILE = "application/x-png&quo

java常用工具类(从开源项目smartframework项目copy过来备用)

1.数组操作工具类 package org.smart4j.framework.util; import org.apache.commons.lang.ArrayUtils; /** * 数组操作工具类 * * @author huangyong * @since 1.0 */ public class ArrayUtil { /** * 判断数组是否非空 */ public static boolean isNotEmpty(Object[] array) { return !ArrayUt