String方法阅读笔记
String类常用方法
1.int Length():
参数:无
返回值:调用此方法的字符串的长度(int)
实例:
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.cnblogs.com");
String Str2 = new String("cnblogs" );
System.out.print("字符串 Str1 长度 :");
System.out.println(Str1.length());
System.out.print("字符串 Str2 长度 :");
System.out.println(Str2.length());
}
}
2.char charAt(int index):
charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
参数:index:字符串索引
返回值:返回指定索引处的字符
实例:
public class Test {
public static void main(String args[]) {
String s = "www.cnblogs.com";
char result = s.charAt(8);
System.out.println(result);
}
}
3.void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):
将字符从此字符串复制到目标字符数组。
参数:
srcBegin -- 字符串中要复制的第一个字符的索引。
srcEnd -- 字符串中要复制的最后一个字符之后的索引。
dst -- 目标数组。
dstBegin -- 目标数组中的起始偏移量。
返回值:无
实例:
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.cnblogs.com");
char[] Str2 = new char[6];
try {
Str1.getChars(4, 10, Str2, 0);
System.out.print("拷贝的字符串为:" );
System.out.println(Str2 );
} catch( Exception ex) {
System.out.println("触发异常...");
}
}
}
4.String replace(char oldChar, char newChar)
replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
参数:
- oldChar -- 原字符。
- newChar -- 新字符。
返回值:替换后生成的新字符串。
实例:
public class Test {
public static void main(String args[]) {
String Str = new String("hello");
System.out.print("返回值 :" );
System.out.println(Str.replace('o', 'T'));
System.out.print("返回值 :" );
System.out.println(Str.replace('l', 'D'));
}
}
5. String toUpperCase()
toUpperCase() 方法将字符串小写字符转换为大写。
参数:无
返回值:字符转换为大写后的字符串。
实例:
public class Test {
public static void main(String args[]) {
String Str = new String("www.cnblogs.com");
System.out.print("返回值 :" );
System.out.println( Str.toUpperCase() );
}
}
6. String toLowerCase()
toLowerCase() 方法将字符串转换为小写。
参数:无
返回值:字符转换为小写后的字符串。
实例:
public class Test {
public static void main(String args[]) {
String Str = new String("WWW.CNBLOGS.COM");
System.out.print("返回值 :" );
System.out.println( Str.toLowerCase() );
}
}
7. String trim():
trim() 方法用于删除字符串的头尾空白符。
参数:无
返回值:删除头尾空白符的字符串。
实例:
public class Test {
public static void main(String args[]) {
String Str = new String(" www.cnblogs.com ");
System.out.print("原始值 :" );
System.out.println( Str );
System.out.print("删除头尾空白 :" );
System.out.println( Str.trim() );
}
}
8. char[] toCharArray():
toCharArray() 方法将字符串转换为字符数组。
参数:无
返回值:字符数组。
实例:
public class Test {
public static void main(String args[]) {
String Str = new String("www.cnblogs.com");
System.out.print("返回值 :" );
System.out.println( Str.toCharArray() );
}
}
String方法阅读笔记的更多相关文章
- JDK源码阅读:String类阅读笔记
String public final class String implements java.io.Serializable, Comparable<String>, CharSequ ...
- C语言程序设计:现代方法阅读笔记
第二十六章 atexit函数允许用户“注册”在程序终止时要调用的函数:atexit(func); 在程序终止后,func函数会被自动调用 clock()函数可以计算程序运行时间 time函数返回当前的 ...
- Detectron2源码阅读笔记-(二)Registry&build_*方法
Trainer解析 我们继续Detectron2代码阅读笔记-(一)中的内容. 上图画出了detectron2文件夹中的三个子文件夹(tools,config,engine)之间的关系.那么剩下的 ...
- JavaScript学习笔记-用于模式匹配的String方法
用于模式匹配的String方法: String支持4种使用正则表达式的方法: seach()用于检索,参数是一个正则表达式,返回第一个与之匹配的子串的位置,找不到则返回-1,如 ...
- Hadoop阅读笔记(七)——代理模式
关于Hadoop已经小记了六篇,<Hadoop实战>也已经翻完7章.仔细想想,这么好的一个框架,不能只是流于应用层面,跑跑数据排序.单表链接等,想得其精髓,还需深入内部. 按照<Ha ...
- Hadoop阅读笔记(六)——洞悉Hadoop序列化机制Writable
酒,是个好东西,前提要适量.今天参加了公司的年会,主题就是吃.喝.吹,除了那些天生话唠外,大部分人需要加点酒来作催化剂,让一个平时沉默寡言的码农也能成为一个喷子!在大家推杯换盏之际,难免一些画面浮现脑 ...
- Hadoop阅读笔记(三)——深入MapReduce排序和单表连接
继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...
- Hadoop阅读笔记(二)——利用MapReduce求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
- Hadoop阅读笔记(一)——强大的MapReduce
前言:来园子已经有8个月了,当初入园凭着满腔热血和一脑门子冲动,给自己起了个响亮的旗号“大数据 小世界”,顿时有了种世界都是我的,世界都在我手中的赶脚.可是......时光飞逝,岁月如梭~~~随手一翻 ...
随机推荐
- 【做题笔记】P2871 [USACO07DEC]手链Charm Bracelet
就是 01 背包.大意:给您 \(T\) 个空间大小的限制,有 \(M\) 个物品,第 \(i\) 件物品的重量为 \(c_i\) ,价值为 \(w_i\) .要求挑选一些物品,使得总空间不超过 \( ...
- opencv:自适应阈值
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- java 面试架构篇
1.非功能需求会考虑哪些? 可用性.扩展性.性能: 2.有没有遇到过建了索引反而变慢的情况? 3.从哪些角度去设计系统? 4.代码中使用过的设计模式?
- Understanding Java Memory Model-理解java内存模型(JVM)
from https://medium.com/platform-engineer/understanding-java-memory-model-1d0863f6d973 Understanding ...
- yii2 components
components顾名思义就是组件的意思,yii默认会根据 components 数组里面的键值去 vendor\yiisoft\yii2\web 里面查找这个键值得类,如果没有找到,再根据这个键值 ...
- ViewModel、LiveData、DataBinding
ViewModel ViewModel的引入 如果系统销毁或重新创建界面控制器,则存储在其中的任何临时性界面相关数据都会丢失.例如,应用的某个 Activity 中可能包含用户列表.因配置更改而重新创 ...
- itest(爱测试) 4.3.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
itest 简介:查看简介 test 开源敏捷测试管理,testOps 践行者.可按测试包分配测试用例执行,也可建测试迭代(含任务,测试包,BUG)来组织测试工作,也有测试环境管理,还有很常用的测试度 ...
- 一张linux光盘查看是哪个版本号的方法
在此查看版本号,方法如下:打开光盘,查找rpm包中的release,就是版本号.
- CapsNet资源
算法源码: https://github.com/xanderchf/pyCapsNet https://github.com/naturomics/CapsNet-Tensorflow 参考文章: ...
- $.extend({},旧的,新的);合并对象,后面的覆盖前面的
$.extend({},旧的,新的):合并对象,后面的覆盖前面的: <script> $(function(){ int={ a:1, b:function(){console.log(' ...