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方法阅读笔记的更多相关文章

  1. JDK源码阅读:String类阅读笔记

    String public final class String implements java.io.Serializable, Comparable<String>, CharSequ ...

  2. C语言程序设计:现代方法阅读笔记

    第二十六章 atexit函数允许用户“注册”在程序终止时要调用的函数:atexit(func); 在程序终止后,func函数会被自动调用 clock()函数可以计算程序运行时间 time函数返回当前的 ...

  3. Detectron2源码阅读笔记-(二)Registry&build_*方法

    ​ Trainer解析 我们继续Detectron2代码阅读笔记-(一)中的内容. 上图画出了detectron2文件夹中的三个子文件夹(tools,config,engine)之间的关系.那么剩下的 ...

  4. JavaScript学习笔记-用于模式匹配的String方法

    用于模式匹配的String方法:   String支持4种使用正则表达式的方法:           seach()用于检索,参数是一个正则表达式,返回第一个与之匹配的子串的位置,找不到则返回-1,如 ...

  5. Hadoop阅读笔记(七)——代理模式

    关于Hadoop已经小记了六篇,<Hadoop实战>也已经翻完7章.仔细想想,这么好的一个框架,不能只是流于应用层面,跑跑数据排序.单表链接等,想得其精髓,还需深入内部. 按照<Ha ...

  6. Hadoop阅读笔记(六)——洞悉Hadoop序列化机制Writable

    酒,是个好东西,前提要适量.今天参加了公司的年会,主题就是吃.喝.吹,除了那些天生话唠外,大部分人需要加点酒来作催化剂,让一个平时沉默寡言的码农也能成为一个喷子!在大家推杯换盏之际,难免一些画面浮现脑 ...

  7. Hadoop阅读笔记(三)——深入MapReduce排序和单表连接

    继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...

  8. Hadoop阅读笔记(二)——利用MapReduce求平均数和去重

    前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...

  9. Hadoop阅读笔记(一)——强大的MapReduce

    前言:来园子已经有8个月了,当初入园凭着满腔热血和一脑门子冲动,给自己起了个响亮的旗号“大数据 小世界”,顿时有了种世界都是我的,世界都在我手中的赶脚.可是......时光飞逝,岁月如梭~~~随手一翻 ...

随机推荐

  1. Wx-小程序中使用伪类选择器实现border-1px

    .borders::before{ position: absolute; left:; top:; content: " "; width: 100%; height: 1px; ...

  2. git 初次push

    1.本地仓库与远程仓库第一次同步时,一直同步不上 最后 git status ,发现有两个文件没提交 提交后再push即可 2.如果不行,再看一下其他情况

  3. Nexus坑人系列-license格式问题

    这种情况一般出现在RMA或者新设备使用的时候.这些时候一般需要安装license,在安装完license的时候,例如我们去配置一些三层特性,例如feature eigrp等,可能会出现设备拒绝了你的命 ...

  4. 【Python】一些函数

    Python 数字类型转换 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. int(x) 将x转换为一个整数. float(x) 将x转换到一个浮点数. ...

  5. Spring错误:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bi

    在使用SSM框架传递多个参数的时候发生如下错误: 原因是因为在传递多个参数的时候没有使用注解@Param,所以才包如下错误: 参考的技术文章:https://blog.csdn.net/sinat_2 ...

  6. Linux环境下mysql报错:bash: mysql: command not found 的解决方法

    # mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令. 如果这个命令不在这个目录下,当然会找不到命令. 我 ...

  7. CentOS6.5_x64卸载系统原有MySQL

    1.查看系统是否存在MySQL的版本 rpm -qa | grep mysql 2.删除老版本的开头文件和库(rpm -e --nodeps XXX) rpm -e --nodeps mysql-5. ...

  8. Django - 在settings配置终端打印SQL语句

    LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'level': 'DE ...

  9. spring 中json使用

    1.json序列化工具 public class JsonUtils { /** * Logger for this class */ private static final Logger logg ...

  10. springboot @Configuration @bean注解作用

    @Configuration注解可以达到在Spring中使用xml配置文件的作用 @Bean就等同于xml配置文件中的<bean> 在spring项目中我们集成第三方的框架如shiro会在 ...