关于String的相关常见方法
package Stirng类;
/**
* String 常见的相关方法摘要
* @author Administrator
*
*/
public class DemoStringMethod {
public static void main(String[] args) {
String s = "hello world";
//charAt(int index) 返回指定索引处的char值
char c =s.charAt(6);
System.out.println(c);//w //compareTo(String anotherString);按字典顺序比较两个字符串
//compareToIgnoreCase(String str);忽略大小写比较
String s1 = "hello guigu";
int result = s.compareTo(s1);
System.out.println(result);//16 assic差值
int result1 = s1.compareTo(s);
System.out.println(result1);//-16 //concat(String str)将指定的字符串连接到此字符串的结尾
System.out.println(s.concat(s1));//hello worldhello guigu
System.out.println(s1.concat(s));//hello guiguhello world //endsWith(String suffix)-----测试此字符串是否以指定的后缀结束
//startsWith(String prefix)----测试此字符串是否以指定的前缀开始
System.out.println(s.endsWith("rld"));//true
System.out.println(s.startsWith("hel"));//true //使用指定的字符集将此String编码为byte序列,并将结果存储到一个新的byte数组中
byte [] arr = s.getBytes();
System.out.println(arr);
// for(byte qq:arr){
// System.out.println((char)qq);
// }
for(int i = 0;i<arr.length;i++){
System.out.println((char)arr[i]);
} //indexOf(int ch)//返回指定字符在此字符串中第一次出现处的索引
System.out.println(s.indexOf("w"));//
System.out.println(s.lastIndexOf("w"));//6 //replace(char oldChar,char newChar)
//返回一个新的字符串,它是通过用newChar 替换此字符串中出现的所有oldChar得到的
System.out.println(s.replace("l", "p"));//hello world------>heppo worpd //toLowerCase() toUpperCase() 转换大小写
System.out.println("AbcdEfG".toLowerCase());
System.out.println("AbcdEfG".toUpperCase()); //trim() 返回字符串的副本,忽略前后空格
System.out.println(" S D F a".trim()); String str1 = "hello";
String str2 = "hello"; String str3 = new String("hello");
String str4 = new String("hello"); System.out.println(str1==str2);//true
System.out.println(str1==str3);//false
System.out.println(str3==str4);//false System.out.println(str1.equals(str2));//true
System.out.println(str1.equals(str3));//true
System.out.println(str3.equals(str4));//true }
}
关于String的相关常见方法的更多相关文章
- String类的常见方法的使用案例
String类的常见方法的使用案例 //使用指定的字符串替换当前字符串中指定的内容 //将helloworld中的o替换为a String s="HelloWorld"; Stri ...
- js String字符串对象常见方法总结
String对象常用来保存文本形式的数据. 其转化方法有二种: String(s) new String(s) String对象方法有: charAt() charCodeAt() concat() ...
- String对象的常见方法
String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...
- 表格对象QTableWidget相关常见方法
QWidget bool close (self)QRect geometry (self)hide (self)int height (self)setStatusTip (self, QStrin ...
- javascript常见方法汇总之一——数组字符串相关
(转载至慕课网) 原文链接:https://www.imooc.com/article/46933 github地址:https://github.com/dorseysen/notes-about- ...
- JDK方法区、元空间区别 & String.intern相关面试题
一.方法区.永久代.元空间 1.方法区.永久代 方法区也是各个线程共享的内存区域,它用于存储已经被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据.方法区域又被称为"永久代& ...
- 面向对象编程(四)继承,概念及super关键字,final关键字,Object类常见方法
继承 概念: ① 继承背后的思想就是基于已存在的类来构建新类; ② 当从已存在类继承时,就重用了它的方法和属性,还可以添加新的方法和属性来定制新类以应对需求; ③ 当从其它类导出的类叫作子 ...
- Java String的相关性质分析
引言 String可以说是在Java开发中必不可缺的一种类,String容易忽略的细节也很多,对String的了解程度也反映了一个Java程序员的基本功.下面就由一个面试题来引出对String的剖析. ...
- Android View各种尺寸位置相关的方法探究
Android View各种尺寸位置相关的方法探究 本来想做一个View间的碰撞检测之类的. 动手做了才发现不是想象的那么简单. 首先,写好了碰撞检测的工具类如下: package com.mengd ...
随机推荐
- JAVA项目复习的一些小细节
使用javadoc完成自动文档,这一段将在后期进行介绍. 类,方法和变量的作用域. 使用修饰符public和修饰符private,对于类和方法来说并没有什么不妥的,但是变量使用public却在某些时刻 ...
- 使用Metasploit入侵windows之自动扫描
最新版本的metasploit为4.0,可以通过官方网站(www.metasploit.com)直接下载,因为是开源的,所以免费. metasploit很好很强大,集成了700多种exploit,但是 ...
- cc2530 T3定时器控制引脚P1_1的亮灭
/**************************************************************************** * 文 件 名: main.c * 作 者: ...
- service引用
已引用 attachmentManager 为例 ApplicationContext applicationContext = WebApplicationContextUtils.getWebAp ...
- [Unity]背包效果-使用NGUI实现物品的拖拽效果Drag
背包效果-使用NGUI实现物品的拖拽效果Drag 效果实现如图 对象层级关系图 PacketCell - Right 对象作为单元格背景 PacketContainer 对象作为单元格容器 Packe ...
- BNU OJ 50997 BQG's Programming Contest
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- vs2008安装opencv2.4.6
最近安装opencv2.4.6,发现犯了一个很愚蠢的错误,在此记录一下. opencv的头文件包含应该位于build文件夹内,而我误将opencv文件夹下的include包含了进去,造成无法找到头文件 ...
- PHP中对mysql预编译查询语句的一个封装
原文地址:http://chen-shan.net/?p=474 为了防止sql注入,我们都使用过mysqli这个类,但是每次都需要绑定参数,绑定结果等,比较麻烦,所以可以把这些重复的语句封装成一个函 ...
- iReport使用教程
http://www.blogjava.net/keweibo/articles/239492.html 原创出处 http://blog.163.com/liushuo216@126/blog/st ...
- Linux 最简单的驱动程序hello world
图片驱动位置:kernel3-10/driver/misc/mediatek/imgsensor/src/mt8127/kd_sensorlist.c 例子:kernel-3.10/driver/mi ...