String中重要方法与字段
下列这段代码已全部包含了我要写的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中重要方法与字段的更多相关文章
- String中intern方法的作用
前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池 ...
- java String 中 intern方法的概念
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- String中concat方法小记
介绍String中的concat方法使用: 日常开发中,经常对字符串进行处理,之前碰到多个字符串拼接,要么使用stringBuilder,要么使用StringBuffer,再或者是直接多个String ...
- C++中 string 中的方法的使用详解
string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法 1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截 ...
- 30天C#基础巩固------this,base,string中的方法,StringBuilder性能
这里主要是记录下自己学习笔记,希望有个地方在以后可以看到自己走过的路. 关于之前多态的知识有一个口诀,很好理解里面的override和new,virtual关键字. "new则隐藏,over ...
- Javascript ----字符串(String)中的方法
涉及字符串时,常用到的几个方法... --------------------------------------------------------------------------------- ...
- String中intern()方法
intren方法:通俗的讲,是将字符串放入常量池中. new出来的字符串是放在堆中,直接赋值的字符串是放在常量池中的. 对字符串做拼接操作,即做“+”运算,分两种情况 (1)表达式右边是纯字符串常量, ...
- String中的方法
1.string s1 = "abcdefghij"; string s2 = "kuo"; Console.WriteLine(s1.Clone()) ...
- java.String中的方法
(String) str.trim() 该方法返回一个复制该字符串的开头和结尾的白色空格去掉,或字符串,如果它没有头或尾空白. (Boolean) str.contains(str1) 判断 str ...
随机推荐
- CPU 100%
http://www.cnblogs.com/xuehong1985/articles/757060.html
- HTML目录
1. 表格标记 2. HTML常用标记 3. 博客园添加访问人数统计 4. 如何快速掌握CSS(各种CSS工具) 5. HTTP协议状态码详解(HTTP Status Code)(转)
- PHP抓取网络数据的6种常见方法
http://www.nowamagic.net/academy/detail/12220245 http://www.nowamagic.net/academy/detail/12220245
- Sql 使用备份还是使用脚本
对sql使用并不熟悉,但是通过项目接触总结出的经验 什么样的场景下选择使用脚本或者备份: 1:当需要创建相关的库的时候使用脚本 2:当需要还原至某个时间点的状态的或者前进至某个版本的数据库的时候就使用 ...
- ZBarSDK扫描二维码
1:如果报引用文件错误的话需要在设置一下:project->Build settings->build active architecture only选项设置为No.Valid Arch ...
- TCP/IP协议原理与应用笔记21:路由选择的方法
1. 路由选择的方法 (1)基本思想 不是收到IP分组后才能为其选路,而是预先获得所有的目的的路由(Routing Protocol) IP报文按预定的路由转发(route table) (2)预定路 ...
- 【Android Api 翻译4】android api 完整翻译之Contacts Provider (学习安卓必知的api,中英文对照)
Contacts Provider 电话簿(注:联系人,联络人.通信录)提供者 ------------------------------- QUICKVIEW 快速概览 * Android's r ...
- 强大的JQuery(三)--操作html与遍历
前两篇博客讲到了JQuery的基础知识以及其动画效果,本篇将为大家介绍jquery操纵html以及jquery的遍历. 一.jquery操作html 1.获取内容和属性 text() - 设置或返回所 ...
- cxGrid使用汇总2
17. 怎样设计多表头的cxGrid? 解决:cxGrid可以解决如下的表头: --------------------------------- | 说明1 | 说明2 | ------------ ...
- hdu 3280 动态规划
思路:dp[i][j]表示区间i,j变为回文串最少的代价. #include<map> #include<set> #include<cmath> #include ...