(Object String 类中的方法练习)
package com.zs.demo1; public class Demo1 {
public static void main(String[] args) {
fun1();
fun2();
fun3();
fun4();
fun5();
fun6();
fun7();
fun8();
fun9();
} private static void fun9() {
// 判断字符串的内容是否相等
//s.equalsIgnoreCase(s1) 不区分大小写的比较
System.out.println("方法9:equals()方法");
String s=new String("hello");
String s1=new String("hello");
boolean b=s.equals(s1);
System.out.println(b); //输出引用类型String时,输出的不是内存地址,是因为String类重写了toString()方法
} private static void fun8() {
// 将字符串转字符数组
System.out.println("方法8:toCharArray()方法");
String s=new String("与子同袍,岂曰无衣");
char[] c=s.toCharArray();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+" ");
}
System.out.println();
} private static void fun7() {
// 将字符串转字节数组
System.out.println("方法7:getBytes()方法");
String s=new String("与子同袍,岂曰无衣");
byte[] b=s.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+" ");
}
System.out.println();
String s1=new String(b);//将字节数组转字符串
System.out.print("字节数组转字符串:");
System.out.println(s1);
} private static void fun6() {
//查找一个字符,没有返回-1;indexOf(char ch)只能查找单个字符的下标
System.out.println("方法6:indexOf()方法");
String s=new String("hello world");
int x=s.indexOf("e");
System.out.println(x); } private static void fun5() {
// 判断一个一个字符串是否有另一个字符串
System.out.println("方法5:contains()");
String s=new String("hello.java");
boolean b=s.contains("ll");
System.out.println(b);
} private static void fun4() {
// 判断一个字符串是否以指定后缀结束
System.out.println("方法4:endswith()方法");
String s=new String("hello.java");
boolean a=s.endsWith(".java");
System.out.println(a);
} private static void fun3() {
//判断一个字符串是否以指定前缀开始,startswith()方法返回布尔型
System.out.println("方法3:startswith()方法");
String s=new String("wwwhello.java");
boolean b=s.startsWith("hello");//测试此字符串是否以指定的前缀开始
System.out.println(b);
boolean e=s.startsWith("hello",3 );//测试从指定索引的位置开始的字符串是否以指定前缀开始
System.out.println(e); } private static void fun2() {
System.out.println("方法2:substring()方法");
// 获取字符串的一部分,substring();
//substring(int beginIndex,int endIndex);包含头不包含尾
String s=new String("fhwuiehfiu");
String s1=s.substring(1, 3);
System.out.println(s1);
//substring(int beginIndex);从开始下标后面的全要
String s2=s.substring(3);
System.out.println(s2); } private static void fun1() {
// int length();返回字符串长度
String s=new String("dshfiuweh");
System.out.println("方法1:返回字符串长度");
System.out.println(s.length()); }
}
/*运行结果:
方法1:返回字符串长度
9
方法2:substring()方法
hw
uiehfiu
方法3:startswith()方法
false
true
方法4:endswith()方法
true
方法5:contains()
true
方法6:indexOf()方法
1
方法7:getBytes()方法
-45 -21 -41 -45 -51 -84 -59 -37 -93 -84 -58 -15 -44 -69 -50 -34 -46 -62
字节数组转字符串:与子同袍,岂曰无衣
方法8:toCharArray()方法
与 子 同 袍 , 岂 曰 无 衣
方法9:equals()方法
true */
(Object String 类中的方法练习)的更多相关文章
- String类中intern方法的原理分析
一,前言 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今 ...
- 2019.4.1今日一练String类中的方法
package com.pjc.objects; replaceAll()方法的理解引出正则表达式import java.util.regex.Patte ...
- 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj 必选项.要被分解的 ...
- String类中toCharArray()方法的用法
该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg: public class class6_3 { public static void main(String arg ...
- C#基础知识学习(2)string类中的方法
1.Compare 比较字符串 用来比较2个字符串的长度大小和值是否相同,相同则返回0,当x比y小返回-1,否则返回1,如果长度相同,且值不同,则返回1,代码如下 public static void ...
- String类中的常用方法
String类 一.转换成String方法 1.public String(); 空参构造 初始化一个新创建的 String 对象,使其表示一个空字符序列 2.public String(byte[] ...
- Java中String类的format方法使用总结
可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...
- object类的equals方法简介 & String类重写equals方法
object类中equals方法源码如下所示 public boolean equals(Object obj) { return this == obj; } Object中的equals方法是直接 ...
- Java——String类中的compareTo方法总结
String类的定义: java.lang 类 String java.lang.Object java.lang.String 所有已实现的接口:Serializable, C ...
随机推荐
- php的符号的排序大小
- Non-UTF-8 code starting with '\xbb' in file
一.错误问题 错误问题:Non-UTF-8 code starting with '\xbb' in file,如图所示: 二.分析问题 原因:程序文件夹中出现中文,运行的时候出现如下错误,导致出错的 ...
- Python迭代和列表生成器
使用for循环遍历list和tuple,这种遍历成为迭代 在如C语言中都是通过下标拿到值,for...in这种方式其实是相同的. 在函数的一节,这样说--->‘求和函数sum(),sum(ite ...
- MapReduce(四)
MapReduce(四) 1.shuffle过程 2.map中setup,map,cleanup的作用. 一.shuffle过程 https://blog.csdn.net/techchan/arti ...
- Qt 设置窗口居中显示和窗体大小
设置窗口居中显示 方法一:在窗口(QWidget类及派生类)的构造函数中添加如下代码: #include <QDesktopWidget> //....... QDesktopWidget ...
- Linux只下载不安装软件包
有时我们并不需要安装软件而只要下载软件包. 包格式 命令 命令所属包 命令下载格式 rpm yumdownloader yum-utils yumdownloader package_name deb ...
- WinForm之窗体应用程序
WinForm之窗体应用程序 基本简单数据库操作(增删改查) using System; using System.Collections.Generic; using System.Windows. ...
- 通过css 实现“瀑布流”
.hot_list{-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -moz-column-gap:7px; -webk ...
- Oracle.数据的增删改、表操作(创建,修改,删除)、数据类型
SELECT ename,dname FROM emp,dept WHERE emp.deptno=dept.deptno; SELECT dname,loc FROM dept; SELECT JO ...
- js将接口返回的数据序列化
<div style={{marginLeft: '80px'}}> <pre> {th ...