StringMisc
//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():获取字串长度 《空格也算他的长度》
- charAt():获取指定位置的字符
- getChars():获取从指定位置起的子串复制到字符数组中(它有四个参数,在示例中有介绍)
- replace():子串替换
- toUpperCase()、 toLowerCase():大小写转换
- trim():去除头尾空格:
- toCharArray():将字符串对象转换为字符数组
StringMisc的更多相关文章
- String常用方法测试
String.Equals()使用方法 用来比较String两个对象所表示的字符串是否相同 public class StringEquals { public static void main(St ...
- String中重要方法与字段
下列这段代码已全部包含了我要写的String类中重要的字段: //StringMisc.java// This program demonstrates the length, charAt and ...
- CharsRefIntHashMap并不比HashMap<String, Integer>快
我模仿lucene的BytesRef写了一个CharsRefIntHashMap,实測效果并不如HashMap<String, Integer>.代码例如以下: package com.d ...
随机推荐
- Hibernate配置文件
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...
- Java开发中经典的小实例-(随机产生验证码)
import java.util.Scanner;public class Test10 { public static void main(String[] args) { // ...
- yii2-更改默认显示的通用主页
在views/layouts/目录下新建一个login.php,然后SiteController中更新下面的方法 public function actionIndex() { $this->l ...
- mac 安装tomcat
一.下载 首先在tomcat官网下载完整版的 tomcat.tar.gz包,Core下 注: zip用于windows操作系统,tar.gz用于unix和linux操作系统. Binary Distr ...
- Greenplum 在Linux下的安装
1.实验环境 1.1.硬件环境 Oracle VM VirtualBox虚拟机软件:三台Linux虚拟机:Centos 6.5:数据库:greenplum-db-4.3.9.1-build-1-rhe ...
- BitMap存储jpg到手机sd卡中
/** * 将BitMap转成Jpeg图片保存到sdcard(便于以后debug调试查看和裁切调试) */ private void saveBitmap(Bitmap bitmap, String ...
- android开发文档工具集(持续更新中...)
http://www.androiddevtools.cn/ android 产品->交互->视觉->开发->测试各种工具地址下载, 各种文档下载应有尽有,强烈推荐. ht ...
- SPSS数据分析—简单线性回归
和相关分析一样,回归分析也可以描述两个变量间的关系,但二者也有所区别,相关分析可以通过相关系数大小描述变量间的紧密程度,而回归分析更进一步,不仅可以描述变量间的紧密程度,还可以定量的描述当一个变量变化 ...
- MYSQL -NOSQL -handlersocket
一个MYSQL的插件,让MYSQL支持NOSQL 好处,跟MYSQL公用数据.比普通CACHE方便.普通CACHE有同步数据问题 坏处,不兼容MEMCAHE,跟MEMCAHE一样没安全控制 编译与安装 ...
- Java Integer的底层优化
看一个程序(腾讯题) public class showMain { public static void main(String[] args){ //Double i1=127.00,i2=127 ...