String类-小用
字符串-string
(1)string在Java中是一个引用类型,string变量可引用一个字符串对象
(2)
例1:
s0,s1,s2引用同一个对象
New创建的两个string是不同的对象只是内容相同。
例2:
s1,s2引用同一个字符串对象,
s1+=”b”;后s1是引用了新的对象,
s1是一个引用值(地址)不能与“ab”比较内容,即引用值不与内容匹配,
equals()方法可比较字符串内容。
(3)字符串比较
一:s1=”abcd”;s2=”abcd”;if(s1==s2)->此处比较的是s1,s2是否引用同一个对象(地址是否相同)。
二:比较字符串内容。 equals()。equalsIgnoreCase()。compareTo()字典法比较,返回值(0->相等,负数,正数)。regionMatches()比较字符串中某一部分是否相等
equals()方法:
(4):字符串查找。
查找某一字符或字串:
indexOf();
lastIndexOf();
查询是否以某字符串开始或结束:
start sWith();
endWith();
(5)常用字符串方法:
1. subString()提取字串;
例:
public class Stringx {
public static void main(String args[]) {
String s="I am a boy.";
String temp=s.substring(0,6);
System.out.println(temp);
}
}
2. concat()连接字串,或直接用“+”连接。
例:
public class Stringx {
public static void main(String args[]) {
String s="I am a";
String temp=" boy!";
System.out.println(s.concat(temp));
}
}
3. String.valueOf(),将其他类型转化为String类型。
例:
public class Stringx {
public static void main(String args[]) {
int a=12345;
String tem=String.valueOf(a);
System.out.println(tem);
}
}
4. Length();
例:
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
System.out.println(tem.length());
}
}
5. charAt();
例:
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
System.out.println(tem.charAt(3));
}
}
6. getChars();
例:
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
char ch[]=new char[10];
tem.getChars(1,5,ch,0);
System.out.println(ch[4]);
}
}
7. replace();
例:replaceAll(String,String);
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
tem=tem.replaceAll(tem, "aaaaa");
System.out.println(tem);
}
}
8. toUpperCase(),toLowerCase();
例:
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
tem=tem.toUpperCase();
System.out.println(tem);
}
}
9. trim();
例:
public class Stringx {
public static void main(String args[]) {
String tem=" aBCdef g ";
tem=tem.trim();
System.out.println(tem);
}
}
10. toCharArray();
例:
public class Stringx {
public static void main(String args[]) {
String tem="abcdefg";
char ch[]=new char[10];
ch=tem.toCharArray();
for(int i=0;i<tem.length();i++)
System.out.print(ch[i]);
}
}
(6)StringBuffer类
String类型中s1=”abcd”,s1+=”ff”,s1前后引用两个不同的字符串对象,String对象创建后内容不可更改。
StringBuffer对象中可更高内容,长度自动改变
方法:
insert()
delete();
append();等
(7)StringBuilder类
(8)Character类
(9)StringTokenizer类
(10)String类实验:字串加密
设计思想:
设置加密/解密的key=x;
加密/解密:改变每一个字符原字符,前移(加),后移(减)key个单位。
程序流程图:
源代码:
import java.util.Scanner;
//字串加密
public class Mystic {
private String str;
public void setStr(String s) {
str=s;
}
public String getStr() {
return str;
}
public void jiami(String s) {
str=s;
for(int i=0;i<s.length();i++) {
str=str.replace(str.charAt(i),(char)(s.charAt(i)+4));
}
}
public void jiemi(String s) {
str=s;
for(int i=0;i<s.length();i++) {
str=str.replace(str.charAt(i),(char)(s.charAt(i)-4));
}
}
public static void main(String[] args) {
System.out.print("enter the string:");
Scanner scan=new Scanner(System.in);
String tem=scan.next();
Mystic mm=new Mystic();
System.out.println("choice:1->加密;2->解密。");
System.out.print("your choice:");
int ch=scan.nextInt();
if(ch==1) {
mm.jiami(tem);
System.out.println("加密后:"+mm.getStr());
}
else {
mm.jiemi(tem);
System.out.println("解密后:"+mm.getStr());
}
}
}
结果截图:
String类-小用的更多相关文章
- 《java入门第一季》之类String类小案例
案例一: /* * 需求:把数组中的数据按照指定个格式拼接成一个字符串 * 举例: * int[] arr = {1,2,3}; * 输出结果: * "[1, 2, 3]" * 分 ...
- 《java入门第一季》之类(String类常见方法小叙)
String类下面的构造方法和一些常见的方法: /* * 字符串:就是由多个字符组成的一串数据.也可以看成是一个字符数组. * 通过查看API,可以知道 * A:字符串字面值"abc&quo ...
- java中的String类的不可变性的小例子
在java语言中,String类具有不可变性,即常量字符串不可更改.下面的一个小例子简单演示相关概念. public class test { public static void main(Stri ...
- 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)
就在近日,Facebook宣布开源了内部使用的C++底层库,总称folly,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能的需求. 这里是folly的github地址:https:// ...
- 关于Java中的String类知识点小总结
Java中的String类知识点 前言 在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串. 如何创建字符串 最简单的方式 String str = "he ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- String类的功能
String类 标红的为较少出现的 1.判断功能 boolean equals(Object obj) :比较字符串内容是否相同,区分大小写 boolean equalsIg ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- C#string类;math类;datetime类
String类: .Length字符的长度 .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格 .ToUpper()全 ...
随机推荐
- DropShadowEffect导致下拉框控件抖动
<!--<Border.Effect> <DropShadowEffect Direction="180" BlurRadius="1" ...
- mongo之map-reduce笔记
package com.sy.demo; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import ...
- unix下网络编程之I/O复用(二)
select函数 该函数允许进程指示内核等待多个事件中的任何一个发生,并仅在有一个或是多个事件发生或经历一段指定的时间后才唤醒它.我们调用select告知内核对哪些描述字(就读.写或异常条件)感兴趣以 ...
- jQuery判断 form表单提交时一些文本框的判断
一: form表单提交时如果表单里有input标签为空那么不提交form表单. <head> <script type="text/javascript"> ...
- eclipse -- propedit 安装.
SSH 项目中.用到国际化文件,Message.properties 但里面的文本都是 unicode 格式的,用起来不谁.而且也不想通过其它工具转码来使用. 可以通过安装 eclipse 的 pro ...
- ORA-28547:connection to server failed, probable Oracle Net admin error错误,解决方法
当用navicat连接oralce数据库时报ORA-28547错误时,直接懵逼了,上网查了资料说是navicat自带的oci.dll文件的版本和服务器端的oralce数据库的版本不一致造成的. 修改O ...
- 设置Nodejs NPM全局路径
Windows下的Nodejs npm路径是appdata 在nodejs的安装目录中找到node_modules\npm\.npmrc文件 修改如下即可: prefix = E:\nodejs\np ...
- 自定义mysql函数时报错,[Err] 1418 - This function has none of DETERMINISTIC......
今天在我执行自定义mysql函数的SQL时发生了错误,SQL如下: /** 自定义mysql函数 getChildList */delimiter //CREATE FUNCTION `pengwif ...
- mongodb collection method
https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/ db.coll_test.getIndexes()# ...
- Even uploading a JPG file can lead to Cross-Site Content Hijacking (client-side attack)!
Introduction: This post is going to introduce a new technique that has not been covered previously i ...