字符串
来源:互联网 发布:业务流程优化 编辑:程序博客网 时间:2024/06/07 08:46
常量对象
字符串常量对象是用双引号括起的字符序列,例如:"你好"、"12.97"、"boy"等。
字符串对象
声明:String s;
创建字符串对象:s=new String("we are students");
或者用一个已创建的字符串创建另一个字符串,例如:String tom= new String(s);
String类较常用构造方法:
String (char a[])
String(char a[], int startIndex, int count)
引用字符串常量对象
string s1,s2;
s1 = "how are you";
s2 = "how are you";
这样,s1,s2具有相同的引用,因而具有相同的实体。
常用方法
public int length():获取一个字符串的长度
public boolean equals(String s)
比较当前字符串对象的实体是否与参数s指定的字符串的实体相同
public boolean startsWith(String s)
public boolean endsWith(String s) 判断当前字符串对象的前缀(后缀)是否参数s指定的字符串
public boolean contains(String s)
判断当前字符串对象是否含有参数指定的字符串s
public int indexOf (String s)
从当前字符串的头开始检索字符串s,并返回首次出现s的位置
public String substring(int startpoint)
获得一个当前字符串的子串
其相关方法:substring(int start ,int end)
public String trim()
得到一个s去掉前后空格后的字符串对象。
字符串和基本数据
java.lang包中的Integer类调用其类方法public static int parseInt(String s)可以将由“数字”字符组成的字符串,如"12356",转化为int型数据。
类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。
可以使用String 类的类方法public static String valueOf(byte n)等将形如123、1232.98等数值转化为字符串。
字符串与字符数组
String 类的构造方法
String(char[])和String(char[],int offset,int length)
分别用字符数组中的全部字符和部分字符创建字符串对象
将字符串中的全部字符存放在一个字符数组中的方法:
public char[] toCharArray()
Demo32.java
public class Demo32 {
public static void main(String[] args) {
String string = "hello world";
char[] c = string.toCharArray();
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
}
转换成字符数组做出的打印
字符串与字符数组
String 类的构造方法
String(char[])和String(char[],int offset,int length)
分别用字符数组中的全部字符和部分字符创建字符串对象
将字符串中的全部字符存放在一个字符数组中的方法:
public char[] toCharArray()
Demo32.java
public class Demo32 {
public static void main(String[] args) {
String string = "hello world";
char[] c = string.toCharArray();
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
}
转换成字符数组做出的打印
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 数据结构之浅析快速排序
- js onclick 传递 this对象 +html自定义属性 data-*
- 定时器
- 量化投资-基本面模型-沪铅多因素模型
- SSL 1637——苹果
- 字符串
- 算法笔试面试高频题之-一(小试牛刀)
- 数据库系统概念
- 通过@ModelAttribute注解封装客户端提交表单参数为一个业务对象
- 对linux信号量的理解以及实现
- 面向服务的体系架构(SOA)
- 设计模式拾荒之适配器模式(Adapter Pattern)
- LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- Disconf —— 来自百度的分布式配置管理平台