Java中String用法

import java.util.StringTokenizer;
import static java.lang.String.valueOf;
/**
 * @author liucong
 * @date 20190708084116
 *
 */
public class StringLeng {
    public static void main(String[] args){
        String str = "Hello World";
        String str1 = "Hello World";
        String str2 = "Hello My World";
        String str3 = "123";
        String str4 = " Hello My World ";
        long long1 = 123123123;
        //输出字符串
        System.out.println("输出字符串");
        System.out.println(str);
        //获取str长度
        System.out.println("获取str长度");
        System.out.println(str.length());
        //判断字符串前缀
        System.out.println("判断字符串前缀");
        System.out.println(str.startsWith("H"));
        System.out.println(str.startsWith("h"));
        //判断字符串后缀
        System.out.println("判断字符串后缀");
        System.out.println(str.endsWith("d"));
        System.out.println(str.endsWith("D"));
        //比较两个字符串
        System.out.println("比较两个字符串");
        System.out.println(str.equals(str1));
        System.out.println(str.equals(str2));
        //把字符串转化为相应的数值
        System.out.println("把字符串转化为int型");
        System.out.println(Integer.parseInt(str3));
        System.out.println("把字符串转化为long型");
        System.out.println(Long.parseLong(str3));
        System.out.println("把字符串转化为float型");
        System.out.println(Float.valueOf(str3).floatValue());
        System.out.println("把字符串转化为double型");
        System.out.println(Double.valueOf(str3).doubleValue());
        //将数值转化为字符串
        System.out.println("将数值转化为字符串");
        System.out.println(getType(valueOf(1)));
        System.out.println(valueOf(1));
        //字符串检索
        System.out.println("字符串检索,如果没有检索到,将返回-1:");
        System.out.println(str.indexOf("o"));
        //得到字符串的子字符串
        System.out.println("从4处开始获取:");
        System.out.println(str.substring(4));
        System.out.println("从3到6中间的字符:");
        System.out.println(str.substring(3,6));
        //替换字符串中的字符,去掉字符串前后空格
        System.out.println("替换字符串中的字符:");
        System.out.println(str.replace("Hello","Hi"));
        System.out.println("去掉字符串前后空格:");
        System.out.println(str4.trim());
        //分析字符串
        StringTokenizer st = new StringTokenizer(str, " b");
        //countTokens()方法仅能用在StringTokenizer方法之后
        System.out.println("得到一共有多少个语言符号:"+st.countTokens());
        System.out.println("分析字符串:");
        //只要字符串还有语言符号将返回true,否则返回false
        while(st.hasMoreElements()){
            //得到一共有多少个语言符号
            //逐个获取字符串中的数据
            System.out.println(st.nextToken());
        }
    }
    //获取变量类型方法
    public static String getType(Object o){
        //使用int类型的getClass()方法
        return o.getClass().toString();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值