关于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 ...
随机推荐
- HTTP代理浅说
简单的说HTTP代理就是处于HTTP客户端和服务器端之间,中转消息的中间人. 一种代理是代客户端去请求服务器,叫做Forward Proxy正向代理:另一种是代理真正的服务器来接收用户请求,叫做Rev ...
- ThinkPHP3.2.3版本框架could not find driver错误
ThinkPHP3.2.3版本框架could not find driver错误 在更新ThinkPHP框架 3.2.3 时出现错误 解决方法如下: 修改php.ini文件 extension=php ...
- ZooKeeper概述
1.Zookeeper概述 Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoop 的分布式协调服务.它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置 ...
- 禁止root远程登录
Linux禁止root远程登录 ssh的修改vi /etc/ssh/sshd_config将默认的#PermitRootLogin yes去注释改为PermitRootLogin no service ...
- LayoutInflater 类的使用
转 http://yxwang0615.iteye.com/blog/1711147 一个Activity里如果直接用findViewById(),对应的是setConentView()的那个layo ...
- 转 oraenv
代码如下: lines, characters #!/bin/sh # # Get the machine type and then set up ORATAB and TNS_ADMIN vari ...
- CentOS 单用户登录&命令行、图像界面
如何单用户登录: 这是一个很简单的问题,以前没重视,每次linux服务器无法正常启动时,都找应急盘,想偷懒,反而浪费了时间. 今天备忘如下: 1.系统启动时,按光标键调出GRUB引导菜单. 2.选定一 ...
- Qt5:窗口居中显示
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以move((desktop->width ...
- s15day14 ssh秘钥远程连接
1 使用密钥登录 1.1 创建密钥对 1.2 上传公钥文件 1.3 导入公钥信息 1.4 使用密钥对登录2 远程执行命令 2.1 简单命令 2.2 使用脚本执行多命 ...
- Struts2--DomainModel接收参数---使用广泛!!!
1. JSP文件调用格式: <a href="user/user!add?user.name=a&user.age=8">添加用户</a> 2. s ...