04-String课后动手动脑
一.String.equals()方法
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
public String() {
this.value = new char[0];
}
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
二.String 方法使用说明:
1.Length():返回当前字符串长度
用法:int 变量名=字符串名.length();
2.charAt(int index): 取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1。
例:String s=new String(“abcde”); 能得到s.charAt(4)==’e’;
3.getChars():从这个字符串中的字符复制到目标字符数组
用法:字符串名.getChars()
4.replace(char oldChar,char newChar):将字符串中第一个oldChar替换成newChar.
5.toUpperCase():用于把字符串转换为大写。
用法:字符串名.toUpperCase()
6.toLowerCase():方法返回一个字符串,该字符串中的字母被转换为小写字母。
用法:字符串名.toLowerCase()
7.trim():调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了
例:String s=" Hello World ".trim();就是把"Hello World"放入s中。
8.toCharArrary():将该String对象转化为char数组
例:char []c=字符串名字.toCharArray();
转载于:https://www.cnblogs.com/mqlblog/p/7742462.html
04-String课后动手动脑相关推荐
- 03-类与对象——课后动手动脑
1.早期我们经常这样定义变量 int value=100: 前面的示例中这样定义变量 MyClass obj = new MyClass( ...
- JAVA语法基础 动手动脑及课后作业
动手动脑1: 仔细阅读示例: EnumTest.java,运行它,分析运行结果? public class EnumTest { public static void main(String[] ar ...
- JAVA课上动手动脑问题以及课后测试1,2总结
课堂测试1 像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 "软件" 2 (1)题目避免重复 (2)可定制(数量/打印方式) 设计思路 1)产生两 ...
- 04_类与对象_课程动手动脑问题以及课后实验性问题及解答集锦
Answer: 动手动脑: 1--以下代码为何无法通过编译?哪儿出错了? Answer: 因为类Foo的构造函数是有一个参数的,所以我们在new一个Foo类的对象时必须赋予一个符合条件的实参. 2-- ...
- 动手动脑及课后实践3
1. 动手动脑1: (一)运行结果: 分析原因:在Java中,内容相同的字串常量("Hello")只保存一份以节约内存,所以s0,s1,s2实际上引用的是同一个对象. 编译器 ...
- JAVA语法基础作业——动手动脑以及课后实验性问题(一)
一.枚举类型 public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Si ...
- 03-方法ppt动手动脑问题及课后实验性问题总结
一.如何不使用static来进行调用函数? 1.自己定义的不需要都是static,但是要在主函数中调用就需要static了,因为main是静态 的,在类加载时就加载了.如果想用又不加可以吧自己写的方法 ...
- 第三次实验及动手动脑
//第一个动手动脑 package 动手动脑3;public class ClassAndObjectTest {public static void main(String[] args) {//创 ...
- java用对话框输出计算结果_动手动脑及java程序之用消息框进行数的运算
动手动脑自信成就人生 动手动脑1 ✿仔细阅读示例: EnumTest.java,运行它,分析运行结果? package demo; public class Test { public static ...
最新文章
- FFT ---- 2021牛客多校第一场 H Hash Function
- Java知识系统回顾整理01基础04操作符07Scanner
- 8.图片组件和动画效果--从零起步实现基于Html5的WEB设计器Jquery插件(含源码)...
- 国产linux 中标麒麟安装.net core sdk
- 十三、开多线程,咱们一起来斗图
- 文件按m3u8顺序合并_在线视频下载之m3u8篇
- 在VC中 改变List列表中一行的颜色
- android Rect类的使用
- Flink – JobManager.submitJob
- mysqldump 的常用参数。
- Shell 脚本知识回顾 (六) —— Shell 函数
- CTSC2017APIO2017
- php对接银行接口,php 银行接口开发写法
- 运维之Linux秋招重点(根据面经和常见笔试题总结,持续更新)
- mysql 新增字段 添加字段 删除字段 修改字段 级联删除 级联更新 等
- CentOS7源码安装Python3
- 单电路板双W5500驱动代码,基于WIZnet官方驱动修改,STM32H743/H750
- linux下实现文件双向同步 unsion,unison做数据双向同步
- 线性系统实验:化学方程式配平 与 天体轨道参数估计
- the remote end hung up unexpectedly Everything up-to-date