<--------------------------常用的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 安装并启动 ...
随机推荐
- iOS 10跳转到其他app
- (BOOL)jumpsToThirdAPP:(NSString *)urlStr{ if ([urlStr hasPrefix:@"mqq"] || [urlStr hasPr ...
- :状态模式:GumballMachine
#ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...
- spring的官方文档地址
https://docs.spring.io/spring/docs/current/spring-framework-reference/
- 杭电1004 ac code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define STR_LEN 256 str ...
- postman工具测试接口
本篇文章主要介绍怎么在postman工具中进行接口的测试? 从以下几个方面进行介绍: 1.先介绍下接口测试 2.不同类型的接口请求方式如何在postman中进行测试 1.1 接口 什么是接口? 接口一 ...
- 四、使用汇编编写LED裸机驱动
1. 确定硬件连接 打开OK6410底板电路图,找到LED,可以发现NLEDx为0时LED点亮. 找到LED的控制引脚,发现LED控制引脚通过连接器连到了核心板: 打开核心板电路图,找到对应的连接器中 ...
- myql update from 语句
(6)UPDATE 语句与 SELECT 语句中的 TOP 子句一起使用对来自表 authors 的前十个作者的 state 列进行更新 UPDATE authors SET state = 'ZZ' ...
- python django字段类型
<1> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的最大字符 ...
- 4.App测试与Web测试的不同
注释:*蓝色为不同点,红色为测试类型* 测试工具不同 Web自动化用Selenium APP自动化用Appium 软件架构不同 App为C/S架构 Web为B/S架构 需要进行安装卸载更新测试 第一次 ...
- 【Think in java读书笔记】序列化
Java的对象序列化将那些实现了Serializable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复成为原来的对象. 序列化机制能自动弥补不同操作系统之间的差异,也就是说在Wind ...