//StringMisc.java

// This program demonstrates the length, charAt and getChars

// methods of the String class.

//

// Note: Method getChars requires a starting point

// and ending point in the String. The starting point is the

// actual subscript from which copying starts. The ending point

// is one past the subscript at which the copying ends.

import javax.swing.*;

public class StringMisc {

public static void main( String args[] )

{

String s1, output;

char charArray[];

s1 = new String( "hello there" );

charArray = new char[ 5 ];

// output the string

output = "s1: " + s1;

// test the length method

output += "\nLength of s1: " + s1.length();

// loop through the characters in s1 and display reversed

output += "\nThe string reversed is: ";

for ( int i = s1.length() - 1; i >= 0; i-- )

output += s1.charAt( i ) + " ";

// copy characters from string into char array

//四个参数的含义

//1.被拷贝字符在字串中的起始位置

//2.被拷贝的最后一个字符在字串中的下标再加1

//3.目标字符数组

//4.拷贝的字符放在字符数组中的起始下标

s1.getChars( 0, 5, charArray, 0 );

output += "\nThe character array is: ";

for ( int i = 0; i < charArray.length;i++ )

output += charArray[ i ];

JOptionPane.showMessageDialog( null, output,

"Demonstrating String Class Constructors",

JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

}

}

}

  1. Length():获取字串长度  《空格也算他的长度》
  2. charAt():获取指定位置的字符
  3. getChars():获取从指定位置起的子串复制到字符数组中(它有四个参数,在示例中有介绍)
  4. replace():子串替换
  5. toUpperCase()、 toLowerCase():大小写转换
  6. trim():去除头尾空格:
  7. toCharArray():将字符串对象转换为字符数组

StringMisc的更多相关文章

  1. String常用方法测试

    String.Equals()使用方法 用来比较String两个对象所表示的字符串是否相同 public class StringEquals { public static void main(St ...

  2. String中重要方法与字段

    下列这段代码已全部包含了我要写的String类中重要的字段: //StringMisc.java// This program demonstrates the length, charAt and ...

  3. CharsRefIntHashMap并不比HashMap&lt;String, Integer&gt;快

    我模仿lucene的BytesRef写了一个CharsRefIntHashMap,实測效果并不如HashMap<String, Integer>.代码例如以下: package com.d ...

随机推荐

  1. EasyUI中动态生成标签页

    这是最近学到的内容,当时是有思路但是不知道怎么获取当前的点击对象,就没有实现功能,通过更深入的学习,我知道了不仅仅是Java,Oracle中有一个this,同样的EasyUI中也存在一个this,来获 ...

  2. Ubuntu Linux 12.04 LTS amd64系统本地root提权

    URL:http://www.ichunqiu.com/section/173 由于fusermount二进制调用setuid的(geteuid())重置RUID时,它调用/bin/mount才能使用 ...

  3. http知识

    http请求的过程:客户端发起请求,创建端口:http服务器在端口监听客户端请求:http服务器向客户端返回状态和内容. 浏览器搜索自身的DNS缓存-->搜索操作系统自身的DNS缓存(浏览器没有 ...

  4. 通过配置的方式Autofac 《第三篇》

    一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...

  5. Java使用BigDecimal精确计算的简单公式计算器

    由于工作需要,写了一个使用BigDecimal运算的精确计算的计算器(然后发现其实比不用BigDecimal的并好不到哪里去) 只能做加减乘除 double类型的数字在千万级别的时候会转成科学计数法, ...

  6. web应用程序测试方法和测试技术详述

    1.界面测试 现在一般人都有使用浏览器浏览网页的经历,用户虽然不是专业人员但是对界面效果的印象是很重要的.如果你注重这方面的测试,那么验证应用程序是否易于使用就非常重要了.很多人认为这是测试中最不重要 ...

  7. (35)odoo中widget

    widget大全: many2many_tagsone2many_listselectionprogressbarselectionstatusbarhandlemonetarymail_thread ...

  8. B:冷血格斗场

    总时间限制: 1000ms 内存限制: 65536kB描述为了迎接08年的奥运会,让大家更加了解各种格斗运动,facer新开了一家冷血格斗场.格斗场实行会员制,但是新来的会员不需要交入会费,而只要同一 ...

  9. css\html布局及部分知识小分享~~~

    近期发现和总结的知识跟大侠们分享,请大侠们多多评论指教一二?  HTML 1.(1)body需设置页面默认字体大小 body{font-size:12px;} (2)IE6下png图片划过切换失效,建 ...

  10. PR和VV的分类与区别

    Adobe Premiere是一款常用的视频编辑软件,由Adobe公司推出.现在常用的有CS4.CS5.CS6.CC.CC 2014及CC 2015版本.是一款编辑画面质量比较好的软件,有较好的兼容性 ...