Java String字符串方法
1.String构造函数
1> String()
2> String(char[] chars)
String(char[] chars,int startIndex,int numChars)
3> String(String strObj)
4> String(byte asciiChars[])
String(byte asciiChars[],int startIndex,int numChars)
2.整型、字符串相互转换
1> String -> int
(1)int i=Integer.parseInt(String s)
(2)int i=Integer.valueOf(str).intValue()
2>int -> String
(1)String s=String.valueOf(i)
(2)String s=Integer.toString(i)
(3)String s=””+i;
代码
//String --> int
String m_str="123";
int i=Integer.parseInt(m_str);
System.out.println("number is :"+(i+1));
i=Integer.valueOf(m_str);
System.out.println("number is :"+(i+2));
//String --> double
m_str="123.4";
Double d=Double.parseDouble(m_str);
System.out.println("number is :"+(d+3));
//int --> String
int j=321;
String s=String.valueOf(j);
System.out.println("string is :"+s+1);
s=Integer.toString(j);
System.out.println("string is :"+s+2);
s=""+j;
System.out.println("string is :"+s+3);
截图

3.字符串处理函数
1>提取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
截图

2>字符串连接
String concat(String str)
代码
String str="live";
String ss;
ss=str.concat(" nihao");
System.out.println(ss);
System.out.println(str);
结果
live nihao
live
截图

3>字符串长度
int length()
4>求字符串中某一位置的字符
char charAt(int index)
代码
String str="powerful";
char c=str.charAt(3);
System.out.println(c);
System.out.println(str);
结果
e
powerful
5>字符串的比较 (StringBuffer未重写equals方法)
(1) int compareTo(String anotherString)
代码
String str="powerful";
String cstr="powerful";
String costr="poaer";
String comstr="pozes";
if(str.compareTo(cstr)==0)
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
if(str.compareTo(costr)>0)
{
System.out.println("str>costr");
}
else if(str.compareTo(costr)<0)
{
System.out.println("str<costr");
}
else
{
System.out.println("str==costr");
}
if(str.compareTo(comstr)>0)
{
System.out.println("str>comstr");
}
else if(str.compareTo(comstr)<0)
{
System.out.println("str<comstr");
}
else
{
System.out.println("str==comstr");
}
结果
str==cstr
str>costr
str<comstr
截图

(2) boolean equals(Object anObject) //大小写不相等
代码
String str="powerful";
String cstr="powerful";
String costr="poaer";
if(str.equals(cstr))
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
if(str.equals(costr))
{
System.out.println("str==costr");
}
else
{
System.out.println("str!=costr");
}
结果
str==cstr
str!=costr
截图

(3) boolean equalsIgnoreCase(String anotherString)
代码
String str="powerful";
String cstr="poWErFul";
if(str.equalsIgnoreCase(cstr))
{
System.out.println("str==cstr");
}
else
{
System.out.println("str!=cstr");
}
截图

(4) regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
regionMatches(boolean ignoreCase ,int startIndex,String str2,int str2StartIndex,int numChars)
代码
String str="Appl pineApple ppEN ";
String s="Apple";
if(str.regionMatches(9,s,0,s.length()))
{
System.out.println("str中子串与s相等");
}
else
{
System.out.println("str中子串与s不等");
}
if(str.regionMatches(5,s,0,s.length()))
{
System.out.println("str中子串与s相等");
}
else
{
System.out.println("str中子串与s不等");
}
结果
str中子串与s相等
str中子串与s不等
截图

代码
String str="Appl pineApple ppEN ";
String s="apple";
if(str.regionMatches(true,9,s,0,s.length()))
{
System.out.println("str中子串与s相等");
}
else
{
System.out.println("str中子串与s不等");
}
if(str.regionMatches(9,s,0,s.length()))
{
System.out.println("str中子串与s相等");
}
else
{
System.out.println("str中子串与s不等");
}
截图
6>判断字符串的前缀和后缀
(1)boolean startsWith(String prefix) 区分大小写
代码
String string = "powerful";
String preString="power";
if(string.startsWith(preString))
{
System.out.println("preString是string的前缀");
}
else
{
System.out.println("string的前缀不是preString");
}
截图

(2) boolean startsWith(String prefix, int toffset)
代码
String string = "powerful";
String preString = "wer";
if (string.startsWith(preString, 2)) {
System.out.println("preString是string的前缀");
} else {
System.out.println("string的前缀不是preString");
}
截图

(3) boolean endsWith(String suffix)
代码
String string = "powerful";
String afterString = "ful";
if (string.endsWith(afterString)) {
System.out.println("afterString是string的后缀");
} else {
System.out.println("string的前缀不是afterString");
}
截图

7>字符串单个字符的查找
(1) int indexOf(char ch)
代码
String str="powerful";
int c=str.indexOf(4);
System.out.println(c);
c=str.indexOf('e');
System.out.println(c);
截图

(2) int indexOf(char ch, int fromIndex)
代码
String str="pineapple len";
int c=10;
c=str.indexOf('p');
System.out.println(c);
c=str.indexOf('p',2);
System.out.println(c);
c=str.indexOf('p',4);
System.out.println(c);
c=str.indexOf('p',6);
System.out.println(c);
c=str.indexOf('p',7);
System.out.println(c);
截图

(3) int lastIndexOf(char ch)
int lastIndexOf(char ch, int fromIndex)
代码
String str="an pineapple pen";
int c=10;
c=str.lastIndexOf('p');
System.out.println(c);
c=str.lastIndexOf('p',2);
System.out.println(c);
c=str.lastIndexOf('p',4);
System.out.println(c);
c=str.lastIndexOf('p',8);
System.out.println(c);
c=str.lastIndexOf('p',11);
System.out.println(c);
截图

8>字符串子串的查找
(1) int indexOf(String str)
int indexOf(String str, int fromIndex)
代码
String str="apple pineapple pen";
int c=10;
c=str.indexOf("le");
System.out.println(c);
c=str.indexOf("le",5);
System.out.println(c);
c=str.indexOf("le",14);
System.out.println(c);
c=str.indexOf("el");
System.out.println(c);
截图

(2) int lastIndexOf (String str)
int lastIndexOf (String str, int fromIndex)
代码
String str="apple pineapple pen";
int c=10;
c=str.lastIndexOf ("le");
System.out.println(c);
c=str.lastIndexOf ("le",8);
System.out.println(c);
c=str.lastIndexOf ("le",14);
System.out.println(c);
c=str.lastIndexOf ("le",2);
System.out.println(c);
c=str.lastIndexOf ("el");
System.out.println(c);
截图

9>字符串中字符大小写的转换
(1) String toLowerCase() 转换成小写
(2) String toUpperCase() 转换成大写
代码
String str="Apple pineApple PEN";
String upperStr=str.toUpperCase();//大写
String lowerStr=str.toLowerCase();//小写
System.out.println("原始:"+str);
System.out.println("大写:"+upperStr);
System.out.println("小写:"+lowerStr);
截图

10>字符串中多余空格的去除
String trim() //仅是去除开头和结尾的空格,字符串里的空格保留
代码
String str=" Apple pineApple PEN ";
String trimStr=str.trim();
System.out.println("an"+str+"u like");
System.out.println("an"+trimStr+"u like");
截图

11>字符串中字符的替换
(1) String replace(char oldChar,char newChar)
代码
String str=" Apple pineApple PEN ";
String replaceStr=str.replace('p','o');
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
截图

(2) String replaceFirst(String regex, String replacement)
代码
String str=" Apple pineApple PEN ";
String replaceStr=str.replaceFirst("pp","live");
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
结果
an Apple pineApple PEN u like
an Alivele pineApple PEN u like
截图

(3) String replaceAll(String regex, String replacement)
代码
String str=" Apple pineApple ppEN ";
String replaceStr=str.replaceAll("pp","live");
System.out.println("an"+str+"u like");
System.out.println("an"+replaceStr+"u like");
结果
an Apple pineApple ppEN u like
an Alivele pineAlivele liveEN u like
截图

12>字符串转换成字符数组
toCharArray ()
代码
String str="Appl pineApple 苹果";
char[] ch=str.toCharArray();
System.out.println("str:"+str);
System.out.println("ch:"+ch);
System.out.println("ch:");
for(int i=0;i<str.length();i++)
{
System.out.print(ch[i]);
}
System.out.println();
System.out.println("ch[7]:"+ch[7]);
System.out.println("ch[16]:"+ch[16]);
结果及注意
str:Appl pineApple 苹果
ch:[C@15db9742
ch:
Appl pineApple
ch[7]:n
此结果得出的非常不稳定,不知道是不是sublime text软件的问题,还是程序的问题?(欢迎每个人去测试,然后评论,大家一起学习进步)
截图


Java String字符串方法的更多相关文章
- Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0
课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值 String str=& ...
- Java String.contains()方法(转载)
Java String.contains()方法 Java String.contains()方法用法实例教程, 返回true,当且仅当此字符串包含指定的char值序列 描述 java.lang.St ...
- Java String.replace()方法
Java String.replace()方法用法实例教程, 返回一个新的字符串,用newChar替换此字符串中出现的所有oldChar 声明 以下是java.lang.String.replace( ...
- Java String 字符串操作小结
// 转载加编辑 -- 21 Apr 2014 1. Java字符串中子串的查找 Java中字符串中子串的查找共有四种方法,如下: 1.int indexOf(String str) :返回第一次出现 ...
- Java String lastIndexOf() 方法
Java String lastIndexOf() 方法 测试代码 public class Test { public static void main(String[] args) { // -- ...
- JavaScript String 字符串方法
JavaScript String 字符串方法汇总 1.str.indexOf() 方法查找字符串中的字符串 返回 字符串中指定文本首次出现的索引(位置) JavaScript ...
- Java String.compareTo()方法
描述:java.lang.String.compareTo() 方法比较两个字符串的字典. 比较是基于字符串中的每个字符的Unicode值.此String对象表示的字符序列的 参数字符串表示的字符序列 ...
- java String.split方法是用注意点(转)
转自:http://www.blogjava.net/fanyingjie/archive/2010/08/05/328059.html 在java.lang包中有String.split()方法,返 ...
- java String字符串——进度1
String字符串 在JAVA中提供了多种创建字符串对象的方法,这里介绍最简单的两种, 第一种是直接赋值, 第二种是使用String类的构造方法: 如下所示: Strin ...
随机推荐
- PHP面向对象.__set(),__get(),__isset(),__unset()四个方法的
一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...
- sql server之数据库语句优化
三.只返回需要的数据 返回数据到客户端至少需要数据库提取数据.网络传输数据.客户端接收数据以及客户端处理数据等环节,如果返回不需要的数据,就会增加服务器.网络和客户端的无效劳动,其害处是显而易见的,避 ...
- Net分布式系统之四:RabbitMQ消息队列应用
消息通信组件Net分布式系统的核心中间件之一,应用与系统高并发,各个组件之间解耦的依赖的场景.本框架采用消息队列中间件主要应用于两方面:一是解决部分高并发的业务处理:二是通过消息队列传输系统日志.目前 ...
- CSS的两大重点
一.属性:通过属性的复杂叠加才能做出漂亮的网页 二.选择器:通过选择器找到对应的标签设置样式,选择器的作用是:选择对应的标签,为之添加样式 1>标签选择器:根据标签签名找到标签 div{ ...
- VC++ 如何使窗体最大化或是最小化
最大化最小的使得的函数是 ShowWindow函数 ShowWindow(SW_SHOWMINIMIZED);//最小化 ShowWindow(SW_SHOWMAXIMIZED);//最大化 Show ...
- 我的第一个web应用开发搭建-环境配置
MyEclipse 2014 破解图文详细教程 MyEclipse作为Java EE最受欢迎的IDE,最新版本为2014版,MyEclipse 2014破解的方法. 一.安装完成MyEclipse20 ...
- 正确理解javascript当中的面向对象
认识面向对象: 为了说明 JavaScript 是一门彻底的面向对象的语言,首先有必要从面向对象的概念着手 , 探讨一下面向对象中的几个概念: 1.万物皆为空:万物皆对象 2.对象具有封装和继承特性 ...
- js event 事件冒泡和事件捕获详细介绍
. 参考: http://www.jb51.net/article/42492.htm 图: 假设一个元素div,它有一个下级元素p.<div> <p>元素</p> ...
- Linux(Centos) 安装windows字体
有时候在Linux中需要用到windows字体,比如微软雅黑字体,这个时候,可能就需要我们手动去安装字体了(当然,如果服务器上没装过),简单几步如下: 1.在$WINDOWS/Fonts目录中找到对应 ...
- 如何让textarea不可拖拽
文本域textarea有一个特性,就是可以拖拽改变其大小,但是在布局严格要求的页面中,这种特性显然会影响布局. 一行代码就可以搞定: textarea{ resize:none;} 但是在chrome ...