<--------------------------常用的API方法------------------------------>
//1.int length(); 返回的是字符串的长度
public static void fun1() {
String string = "string";
int i = string.length();
System.out.println(i);
}
//2.substring (int beginIndex,int endIndex)获取字符串的一部分 包含头不包含尾
//substring (int beginIndex)后面的全要
public static void fun2(){
String s ="hello word";
String s2=s.substring(2);//下标为2的后面全要
String s1=s.substring(1,4);//包含头不包含尾
System.out.println(s2);
}
//3.boolean startsWith(String preFix)判断一个字符串是否包含另一个字符串
public static void fun3(){
String s="hello word ";
boolean s2=s.startsWith("hello");
System.out.println(s2);
}
//4.判断一个字符串的后缀 结尾 endsWith("参数");
public static void fun4(){
String s ="hello.java";
boolean s1=s.endsWith(".java");
System.out.println(s1);
}
//5.contains判断一个字符串是否有另一个字符串
public static void fun5(){
String s ="hello.java";
boolean s1=s.contains("ll");
System.out.println(s1);
}
//6.查找一个字符 indexOf(char ch)返回int 返回-1没有找到
public static void fun6(){
String s="hello.java";
int s1=s.indexOf('w');
System.out.println(s1);
}
//7.将字符串转为字节数组 getBytes();
public static void fun7(){
String s="今晚十一点见";
byte[] s1=s.getBytes();
//System.out.println(s1);
for (int i=0;i<s1.length;i++){
System.out.println(s1[i]);
}
}
//8.将字符串转为字符数组 toCharArray()
public static void fun8(){
String s ="今晚十一点见";
char[] s1=s.toCharArray();
for(int i=0;i<s1.length;i++){
System.out.println(s1[i]);
}
}
//9.boolean equals(Object obj);判断字符串里面完全相等 返回true
//s.equalsIgnoreCase(s1) 不区分大小写的比较
public static void fun9(){
String s="hello";
String s1="Hello";
System.out.println(s.equals(s1));
System.out.println(s.equalsIgnoreCase(s1));
}
//10.将字符数组转换为字符串(字符数组参数不查询编码)
public static void fun10(){
char[] ch = {'a','b','c','d','e','f'};
String s= new String(ch);
String s1= new String(ch,1,2);
System.out.println(s1);
}
//11.将字节数组转换为字符串
public static void fun11(){
byte[] bytes={-67,-15,-51,-19,-54,-82,-46,-69,-75,-29,-68,-5};
//调用String的构造方法,传递字节数组,字节数组转字符串(查unicode码);
String str=new String(bytes);
System.out.println(str);
byte[] bytes1={-67,-15,-51,-19,-54,-82,-46,-69,-75,-29,-68,-5};
//调用构造方法,传递数组,传递两个int x,y 代表截取,截取的位置,x代表下标开始的位置,y代表结束位置
String s=new String(bytes1,0,12);
System.out.println(s);
}
//12.字符串的首字母转成大写,其他小写 toUpperCase()转大写 toLowerCase()转小写
public static void fun(){
String s ="charAt(0)";
//获取字符串的一部分,包含头不包含尾
String s1=s.substring(0,1);
//转大写
String s2=s1.toUpperCase();
//System.out.println(s2);
//下标为1的及1后面的都要
String s3=s.substring(1);
//转小写
String s4=s3.toLowerCase();
System.out.println(s2+s4);
}
}
★ final(最终)关键字的使用:
在定义的类上加上修饰符final,则该类是最终类,不能有子类,不能被继承。但是使用方式没有变化,例如创建对象和调用方法。
public final class Fu {
public void show(){
System.out.println("父类的最终方法");
}
}
public class Test {
public static void main(String[] args) {
Fu f = new Fu();
f.show();
} }
★equals关键字的使用(判断是否相等)
将父类的方法equals方法重写,不改变父类的源代码,equals比较内存地址。比较两个成员变量,变量值相等就返回true,不等返回false。重写父类的方法equals,自己定义对象的比较方式。
Person:
public class Person extends Object{
private String name;
private int age;
public Person(){}
public Person(String name,int age){
this.name=name;
this.age=age;
}
/*
* 重写Object的toString()方法 没有必要让用户看到内存地址
* 要求 :返回值是成员变量的
* */
public String toString(){
return name+": "+age;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
//对obj 作非空判断
if(obj==null){
return false;
}
if(obj instanceof Person){
//参数obj 接受到的是 Person对象 才能转型
//对obj这个参数进行向下转型
Person p =(Person)obj;
return this.age==p.age;
}
return false;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
Test:
public class Test {
public static void main(String[] args) {
Person p = new Person("张无忌",18);
Person p1 = new Person("张san",18);
boolean e = p1.equals(p1);
System.out.println(e); boolean b = p.equals(p1);//重写了父类equals方法 比较年龄 如果不重写 比较内存
System.out.println(b);
//调用Person类的方法toString()
//输出语句 写的是每一个对象 默认就调用toString(); System.out.println(p);
System.out.println(p1);
}
}
★Srting 类的特点
一切都是对象,一旦被创建不能修改。
public class StringDemo {
public static void main(String[] args) {
String str="qy97";
System.out.println(str);
str="love java";
System.out.println(str);
}
}
运行结果:
<--------------------------常用的API方法------------------------------>的更多相关文章
- 简单物联网:外网访问内网路由器下树莓派Flask服务器
最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...
- 利用ssh反向代理以及autossh实现从外网连接内网服务器
前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...
- 外网访问内网Docker容器
外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...
- 外网访问内网SpringBoot
外网访问内网SpringBoot 本地安装了SpringBoot,只能在局域网内访问,怎样从外网也能访问本地SpringBoot? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装Java 1 ...
- 外网访问内网Elasticsearch WEB
外网访问内网Elasticsearch WEB 本地安装了Elasticsearch,只能在局域网内访问其WEB,怎样从外网也能访问本地Elasticsearch? 本文将介绍具体的实现步骤. 1. ...
- 怎样从外网访问内网Rails
外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...
- 怎样从外网访问内网Memcached数据库
外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...
- 怎样从外网访问内网CouchDB数据库
外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...
- 怎样从外网访问内网DB2数据库
外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...
- 怎样从外网访问内网OpenLDAP数据库
外网访问内网OpenLDAP数据库 本地安装了OpenLDAP数据库,只能在局域网内访问,怎样从外网也能访问本地OpenLDAP数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动 ...
随机推荐
- VSTO:使用C#开发Excel、Word【10】
第二部分:.NET中的Office编程本书前两章介绍了Office对象模型和Office PIA. 您还看到如何使用Visual Studio使用VSTO的功能构建文档中的控制台应用程序,加载项和代码 ...
- Cracking The Coding Interview 9.0
#include <iostream> #include <vector> using namespace std; void mswap(int &a, int &a ...
- 读写锁 SRWLOCK
读写锁在对资源进行保护的同时,还能区分想要读取资源值的线程(读取者线程)和想要更新资源的线程(写入者线程). 对于读取者线程,读写锁会允许他们并发的执行.当有写入者线程在占有资源时,读写锁会让其它写入 ...
- 1043 输出PATest
给定一个长度不超过 104 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种 ...
- 国行 lg g3 D858 刷 lg g3 D858hk 教程(备忘)
纯手打,转载请注明出处~ 刷机有风险,出现问题概不负责! 本着自娱自乐的宗旨 ,分享一下,出了问题不负责! 准备的材料: 1,手机一枚(废话)国行lg g3 d858 2,root 工具 用来root ...
- 强震记录和GPS记录,地震波记录的区别
强震记录的是加速度数据,但gps记录的是位移数据.这样的话,强震记录应该说是近场地震数据: 那么, 为什么不干脆用近场的地震波仪器呢,是因为,地震仪记录会限幅,导致记录不全.
- tensorflow-softmax
之前在softmax多分类中讲到多用交叉熵作为损失函数,这里顺便写个例子,tensorlflow练手. # encoding:utf-8 import tensorflow as tf import ...
- Beta阶段冲刺---Day5
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 昨天已完成的工作: (1)闯关模式界面设计: (2)主界面做了相应修改: (3)RankActivity修改. (4)RANKli ...
- 对象存储到session中
以前在使用java开发中,通常都是在session里面存放的对象.在使用php开发中,也打算在session中存入对象,确实能把对象放进去,也能把整个对象输出,但就是取不出对象里面的属性. 通过pri ...
- 【转载】 Pytorch(0)降低学习率torch.optim.lr_scheduler.ReduceLROnPlateau类
原文地址: https://blog.csdn.net/weixin_40100431/article/details/84311430 ------------------------------- ...