下列这段代码已全部包含了我要写的String类中重要的字段:

//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 );
    }
}

以下是程序运行结果截图:

Length()的用法::获取字串长度

数组有length属性,字符串有length()方法
所以,字符串长度用length();数组可以用length;

java.lang.String.charAt()  用法:   方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推,返回值为char类型不可以用String类型得到。

getChars()用法:获取从指定位置起的子串复制到字符数组中(它有四个参数)

getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)将字符从此字符串复制到目标字符数组。

要复制的第一个字符在索引 srcBegin 处;要复制的最后一个字符在索引 srcEnd-1 处(因此要复制的字符总数是 srcEnd-srcBegin)。要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.例如
String str = "abcdefghikl";
Char[] ch = new char[8];
str.getChars(2,5,ch,0);
就是从str的第二个字母开始一直复制到第五个,一共是3个字符,从ch的第一个开始接受.

replace():子串替换用法:

toUpperCase()、 toLowerCase():大小写转换:

toUpperCase()将小写转换为大写,toLowerCase()将大写转换为小写;()内可以是String char类型的变量名也可以是“字符串”。

trim():去除头尾空格:

toCharArray():将字符串对象转换为字符数组

具体用法:

 String myString="abcd";
char myChar[]=myString.toCharArray();
 

String中重要方法与字段的更多相关文章

  1. String中intern方法的作用

    前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池 ...

  2. java String 中 intern方法的概念

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  3. String中concat方法小记

    介绍String中的concat方法使用: 日常开发中,经常对字符串进行处理,之前碰到多个字符串拼接,要么使用stringBuilder,要么使用StringBuffer,再或者是直接多个String ...

  4. C++中 string 中的方法的使用详解

    string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法 1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截 ...

  5. 30天C#基础巩固------this,base,string中的方法,StringBuilder性能

    这里主要是记录下自己学习笔记,希望有个地方在以后可以看到自己走过的路. 关于之前多态的知识有一个口诀,很好理解里面的override和new,virtual关键字. "new则隐藏,over ...

  6. Javascript ----字符串(String)中的方法

    涉及字符串时,常用到的几个方法... --------------------------------------------------------------------------------- ...

  7. String中intern()方法

    intren方法:通俗的讲,是将字符串放入常量池中. new出来的字符串是放在堆中,直接赋值的字符串是放在常量池中的. 对字符串做拼接操作,即做“+”运算,分两种情况 (1)表达式右边是纯字符串常量, ...

  8. String中的方法

    1.string s1 = "abcdefghij";   string s2 = "kuo";   Console.WriteLine(s1.Clone()) ...

  9. java.String中的方法

    (String) str.trim() 该方法返回一个复制该字符串的开头和结尾的白色空格去掉,或字符串,如果它没有头或尾空白. (Boolean) str.contains(str1) 判断 str ...

随机推荐

  1. Getting NHibernate to generate a HiLo string ID

    We've got a large system that's loosely bound to its data source (Navision) via Unity - we're gettin ...

  2. IPC——匿名管道

    Linux进程间通信——使用匿名管道 在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它 ...

  3. codeblocks中添加-std=c99

    早上用codeblocks编译一个c文件,出现这样一个编译错误: +'for'+loop+initial+declarations+are+only+allowed+in+C99+mode 原来cod ...

  4. C++检测一个文件是否存在

    ifstream::is_open - C++ Reference http://www.cplusplus.com/reference/fstream/ifstream/is_open/ // if ...

  5. 终端I/O之综述

    终端I/O有两种不同的工作模式: 规范模式输入处理(Canonical mode input processing).在这种模式中,终端输入以行为单位进行处理.对于每个读要求,终端驱动程序最多返回一行 ...

  6. centos下编译安装mysql5.6

    CentOS 6.4下编译安装MySQL 5.6.14 参考:http://www.cnblogs.com/xiongpq/p/3384681.html 概述: CentOS 6.4下通过yum安装的 ...

  7. 【开源项目6】介绍MenuDrawer这个牛x的控件,实现左右出菜单,上下出菜单

    现在很多应用都很潇洒的从左边屏幕手势一划出个左边的隐藏菜单,右边一划出个隐藏菜单,上边一划出个隐藏菜单,下边一划出个隐藏菜单.或者像android的API16左右的激活列表项的功能.很多人肯定都很着迷 ...

  8. JAVA中的deflate压缩实现

    在文件的传输过程中,为了使大文件能够更加方便快速的传输,一般采用压缩的办法来对文件压缩后再传输,JAVA中的java.util.zip包中的Deflater和Inflater类为使用者提供了DEFLA ...

  9. jQuery过滤选择器:not()方法使用介绍

    在jQuery的早期版本中,:not()筛选器只支持简单的选择器,说明我们传入到:not这个filter中的selector可以任意复杂,比如:not(div a) and :not(div,a) & ...

  10. codeforces Good Bye 2013 379D New Year Letter

    题目链接:http://codeforces.com/problemset/problem/379/D [题目大意] 告诉你初始字符串S1.S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的 ...