Sting中方法举例
2016-07-30 09:41
295 查看
package com.liaojianya.chapter2; /** * String一些方法的用法。 * 1)声明一个名为name的String对象,内容是“My name is Networkcrazy”; * 2)输出字符串的长度; * 3)输出字符串的第一个字符; * 4)输出字符串的最后一个字符; * 5)输出字符串的第一个单词; *6) 输出字符串crazy的位置(从0开始编号的位置) * @author LIAO JIANYA * 2016年7月30日 */ public class Exercise16_2 { public static void main(String[] args) { String name = "My name is Networkcrazy"; char[] data = name.toCharArray(); System.out.println("length of string is : " + data.length); System.out.println("the first character of string is : " + name.charAt(0)); System.out.println("the last character of string is : " + name.charAt(data.length - 1)); System.out.println("the first word of string is : " + name.substring(0, 2)); String data1[] = name.split(" "); System.out.println("the first word of string is : " + data1[0]); System.out.println("the index of \" crazy \" is " + name.indexOf("crazy")); } }
运行结果:
<pre name="code" class="java">length of string is : 23 the first character of string is : M the last character of string is : y the first word of string is : My the first word of string is : My the index of " crazy " is 18
相关文章推荐
- Activity数据更新,及时通知下面的Fragment
- Delphi下EasyGrid使用体会
- JavaSE学习笔记之-----集合
- 55.A+B Problem III
- [置顶] pull解析请求网络的数据(带分页加载,刷新) xlistview HttpUtils
- 新浪博客,CSDN博客, 博客园, 知乎发表 都可以。开源中国 。
- android sqlite增删改查语句
- puppet时遇到的坑
- 概率图模型学习(一):概率图矩阵分解
- 54.九九乘法表
- 濮阳,明天母亲节,怎么送礼物?团购送礼物。相关资料。20160507
- constructors not allowed a return type错误问题
- Java (for循环综合应用)
- 濮阳买房 房价预估 参考,
- 53.小光棍数
- 泡妞相关技能学习。
- Java NIO vs IO
- HSF和Dubbo有什么区别
- 后缀数组模板
- 练习场acm 题目8 一种排序