//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. c# base关键的理解

    base 最大的使用地方在面相对性开发的多态性上.(什么是多态,如何理解多态) base可以[完成]创建派生类实例时调用其基类构造函数或者调用基类上已经被其他方法重写的方法 //关于base调用基类构 ...

  2. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. INPUT只能输入数字

    input只能输入数字: (只能输入数字,并且输入的值不能大于99),但是这样有个问题,就是当输入非数字字符时,输入框中所有的字符都会被清除 <input type="text&quo ...

  4. flex 4.6 移动开发 app.xml配置说明

    <?xml version="1.0" encoding="utf-8" standalone="no"?> <appli ...

  5. socket初级使用(客户端)

    在国庆这段时间里用零星的一些时间看了一下socket的学习资料,由于笔者偏向学习实用方面的内容,因此此篇文章涉及理论知识较少,主要是以实现思路(怎么做)为主,但在实现之前还是需要了解一些基础的理论知识 ...

  6. php 判断是手机版还是电脑端

    function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { ...

  7. sql server查看正在执行的进程

    SELECT SPID = er.session_id  ,Status = ses.status  ,[Login] = ses.login_name  ,Host = ses.host_name  ...

  8. yii2-basic后台管理功能开发之一:引入模板

    前景:哎没想到用php的yii2框架写了官网……自己选择的框架,跪着也要码完,当年用yii2-basic搭建了官网,原是想偷懒的,没想到还要写后台.... 需求:实现一个可编辑页面图片及新闻内容的后台 ...

  9. App_GlobalResources.afvubzdv.resources.dll”--“拒绝访问。“

    在使用ArcGIS Viewer for Silverlight创建应用程序的时候有时会出现编译错误 前面的忘了  最后是App_GlobalResources.afvubzdv.resources. ...

  10. Android开发--Intent的应用

    1.概述 Intent负责对应用中一次操作的动作,动作涉及的数据,附加的数据进行描述,起到媒介的作用.通过Intent对象指定一个activity,利用startActivity或 startActi ...