//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. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  2. JVM学习——编译OpenJDK

    最近在学习<深入理解java虚拟机 第二版>这本书.书中第一部分建议大家自己编译OpenJDK.抱着学习态度也来编译个玩一玩.下面进入正题. 1.编译环境介绍 操作系统 CentOS Li ...

  3. golang csv问题

    go语言自带的有csv文件读取模块,看起来好像不错,今天玩玩,也算是系统学习go语言的一部分--^_^ 一.写csv文件 函数: func NewWriter(w io.Writer) *Writer ...

  4. Windows2012安装IIS和FTP

    Windows2012安装IIS和FTP  1.打开Windows 2012的服务器管理器,选择 管理→添加角色和功能  2.进入 添加角色和功能向导,选择 “Web服务器(IIS)” 3.根据情况选 ...

  5. netbean快捷键

    1.Application应用程序的参数args的设置,在Build->Set Main Projects Configuration 2.程序运行快捷键F6 3.@Deprecated 4.代 ...

  6. html5-表单

    例子: text,number,email 的输入框 <!-- required:必填项 --> <!-- autofocus:获得焦点 --> <!-- placeho ...

  7. 并发编程 13—— 线程池的使用 之 配置ThreadPoolExecutor 和 饱和策略

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  8. c#调用c++动态链接库的问题

    1. Server Error in '/' Application. Object reference not set to an instance of an object. 这个问题是接口中的方 ...

  9. 两个TextView控件居中显示

    通过一个线性布局将两个TextView控件包装在一起,设置LinearLayout的layout_centerInParent属性为true即可.代码如下 <LinearLayout andro ...

  10. solr windows 启动和关闭命令

    进入bin目录执行  启动:solr -e dih 停止:solr stop -all D:\Solr\solr-5.2.1\solr-5.2.1\bin>solr -e dih