博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String
阅读量:4686 次
发布时间:2019-06-09

本文共 4971 字,大约阅读时间需要 16 分钟。

1 package cn.hello; 2  3 /* 4  * String类概述及其构造方法 5  * 字符串的特点:1字符串字面值可以看成一个字符串对象  2 字符串是常量,一旦被赋值,就不能被改变 6  *             构造方法: 7  *                             public String()     空构造 8  *                             public String(byte[] bytes)   把字节数组转为字符串 9  *                             public String(byte[] bytes,int offset,int length)   把字节数组转为字符串10  *                             public String(char[] value)  :   把字符数组转为字符串11  *                             public String(char[] value, int index, int count):把字符串常量值转为字符串12  *                             public String(String original):把字符串常量值转为字符串13  * 14  *             成员方法15  *                             public int length() :长度16  * */17 18 public class Test01 {19     public static void main(String[] args) {20         String s1=new String();21         //s1="hello world";22         System.out.println(s1.length());23         //public String(byte[] bytes)24         byte[] bys={97,98,99,100,101,102};25         String s2=new String(bys);26         System.out.println(s2);27         String s3=new String(bys,2,3);28         System.out.println(s3);29         30         char[] chs={'a','b','c','d','e','艾'};31         String s4=new String(chs);32         System.out.println(s4);33     }34 }
1 package cn.hello; 2  3 /* 4  * 5  * 字符串是常量,一旦被赋值,就不能被改变 6  * 7  *                                            "hello" 没有变化 ,但s所指的地址值发生了变化 8  * */ 9 10 public class Test01 {11     public static void main(String[] args) {12         String s="hello";13         System.out.println(s.hashCode());14         s+="world";15         System.out.println(s.hashCode());16         System.out.println(s);            //helloworld17         18     }19 }
1 package cn.hello; 2  3 /* 4  * String s=new String("hello")  和String s="hello"的区别 5  * */ 6  7 public class Test01 { 8     public static void main(String[] args) { 9         String s1=new String("hello");10         String s2="hello";11         12         System.out.println(s1==s2);    //比的是地址值13         System.out.println(s1.equals(s2));              //比的是值14         15     }16 }
1 package cn.hello; 2  3 /*字符串的判断功能 4  *     boolean equals(Object obj)   :重写之后比较的是字符串的内容 5  * boolean equalsIgnoreCase(String str)   :不计大小写的比较 6  * boolean contains(String str)  :判断大串是否包含小串 7  * boolean startsWith(String str): 8  * boolean endsWith(String str) 9  * boolean isEmpty()   :判断的是字符串内容是否为空10  * 11  * */12 13 public class Test01 {14     public static void main(String[] args) {15         String s1="";16         System.out.println(s1.isEmpty());17     }18 }
1 package cn.hello; 2  3 /* 4  * 字符串的获取功能 5  *             int length():获取字符串的长度 6  *             char charAt(int index):获取指定索引位置的字符 7  *             int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引 8  *             int indexOf(String str) :返回指定字符串在此字符串中第一次出现处的索引 9  *             int indexOf(int ch,int fromIndex)10  *             int indexOf(String str,int fromIndex)11  *             String substring(int start)            截取字符串12  *             String substring(int start,int end)   13  * 14  * */15 16 public class Test01 {17     public static void main(String[] args) {18         19     }20 }
1 package cn.hello; 2  3 /* 4  *         遍历获取字符串中的每一个字符 5  *                 char charAt(int index) 6  * */ 7  8 public class Test01 { 9     public static void main(String[] args) {10         String s1="hello world take it easy ,so we can go there.";11         for(int i=0;i
1 package cn.hello; 2  3 /* 4  * 统计大小写 5  *          6  *  7  *  8  * */ 9 10 public class Test01 {11     public static void main(String[] args) {12         String s1="HellO wOrld 12F";13         int bigNum=0;14         int smallNum=0;15         int num=0;16         for(int i=0;i
'a' && ch<'z'){19 smallNum++;20 }else if(ch>'A' && ch<'Z'){21 bigNum++;22 }else if(ch>'0' && ch<'9'){23 num++;24 }25 }26 System.out.println("small"+smallNum+", big:"+bigNum+" num: "+num);27 }28 }

 

1 package cn.hello; 2  3 /* 4  * 字符串的转换功能 5  *             byte[] getBytes()  :把字符串转换为字节数组 6  *             char[] toCharArray()  :把字符串转换为字符数组 7  *             static String valueOf(char[] chs):把字符数组转为字符串   8  *             static String valueOf(int i):   把int类型转为字符串 9  *             String toLowerCase10  *             String toUpperCase11  *             String concat(String str)12  * 13  * */14 15 public class Test01 {16     public static void main(String[] args) {17 18     }19 }
1 package cn.hello; 2  3 /* 4  * 字符串的其他功能 5  *     替换功能     6  *     String replace(char old,char new) 7  *         String replace(String old,String new) 8  *  9  * 去除字符串两端空格10  *         String trim()11  * 12  *     按字典顺序比较两个字符串13  *         int   compareTo(String str)14  *         int   compareToIgnoreCase(String str)15  * 16  * */17 18 public class Test01 {19     public static void main(String[] args) {20 21     }22 }

 

转载于:https://www.cnblogs.com/chengling/p/4764957.html

你可能感兴趣的文章
值传递和引用传递
查看>>
不用加减乘除做加法
查看>>
享受清晨
查看>>
Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关
查看>>
分享 21 个最新的超酷 web 设计特效
查看>>
noip2017逛公园
查看>>
「本地搭建」:apache+php+mysql
查看>>
R in action读书笔记(4)-第六章:基本图形(下)
查看>>
五月四日 HTML基础,通用标签
查看>>
微信营销小游戏开发之6月份总结
查看>>
[开发技巧]·TopN指标计算方法
查看>>
【模板】BZOJ 3685: 普通van Emde Boas树——Treap
查看>>
Python yield 使用浅析
查看>>
如果adb devices出现5037端口未被占用依旧显示获取失败,出现以下报错时的解决办法...
查看>>
pyapp的封装和内置函数
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
koa2实现对mysql的增删改查函数封装
查看>>