Java常用类(String类)的简单用法

11-20| http://www.dianzi6.com | EDA/PLD|人气:203

Java常用类(String类)的简单用法

  Java常用类(String类)

  public final class String

  extends Object

  implements Serializable, Comparable<String>, CharSequence

  String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现

  常用方法:

  public char charAt(int index)

  返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。

  序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。

  public int compareTo(String anotherString)

  按字典顺序比较两个字符串

  public String concat(String str)

  将指定字符串连接到此字符串的结尾

  public String replace(char oldChar,

  char newChar)

  返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

  public String[] split(String regex)

  根据给定正则表达式的匹配拆分此字符串。

  public String trim()

  返回字符串的副本,忽略前导空白和尾部空白。

  public char[] toCharArray()

  将此字符串转换为一个新的字符数组

  public int indexOf(String str,                   int fromIndex)返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始下面给出一个实例参考String类的用法

  /**

  * 测试String的常用方法

  */

  package com.basic.string;

  /**

  * @author Administrator

  *

  */

  public class TestString {

  /**

  * 统计字符串中大写字母的个数,小写字母的个数,非字母的个数

  * @param str 字符串

  */

  private static void countString(String str) {

  int countUpperNumber = 0;

  int countLowerNumber = 0;

  int countOtherNumber = 0;

  for (int i=0; i<str.length(); i++) {

  char ch = str.charAt(i);

  if (ch >= 'A' && ch <= 'Z') {

  countUpperNumber ++; //注意++的格式

  } else if (ch >= 'a' && ch <= 'z') {

  countLowerNumber ++;

  } else {

  countOtherNumber ++;

  }

  }

  System.out.println("字符串" + str + "\n大写字母数为:" + countUpperNumber

  + "\t小写字母数为:" + countLowerNumber + "\t非字母数为:" + countOtherNumber);

  }

  /**

  * 输出在一个字符串中指定字符串出现的次数

  * @param src原字符串 sub子串

  */

  private static void count(String src, String sub) {

  int count = 0;

  int subLength = sub.length();

  int srcLength = src.length();

  int index = 0;

  int pos = 0;

  while (index < srcLength) {

  pos = src.indexOf(sub, index);

  if (pos != -1) {

  count ++;

  index = pos;

  index += subLength;

  } else {

  break;

  }

  }

  System.out.println(sub + "在" + src +"中出现" + count + "次");

  }

  /**

  * 测试String中方法的用法

  * @param args

  */

  public static void main(String[] args) {

  String s1 = "hello";

  String s2 = "world";

  String s3 = "hello";

  System.out.println(s1 == s3);  //true 字符常量保存在data segment,一般相同的常量在data segment只有一份

  s1 = new String("hello");

  s2 = new String("hello");

  System.out.println(s1 == s2); //false

  System.out.println(s1.equals(s2)); //true 类String重写了Object的equals方法

  char c[] = {'s', 'u', 'n', ' ', 'j', 'a', 'v', 'a'};

  String s4 = new String(c);

  String s5 = new String(c,4,4);

  System.out.println(s4); //sun java

  System.out.println(s5); //java

  s1 = "sun java";

  s2 = "Sun Java";

  System.out.println(s1.charAt(1)); //u 字符串是从0记数的

  System.out.println(s2.length()); //8

  System.out.println(s1.indexOf("java")); //4

  System.out.println(s1.indexOf("Java")); //-1

  System.out.println(s1.equals(s2)); //false

  System.out.println(s1.equalsIgnoreCase(s2)); //true

  String s = "我是程序员,我在学java";

  String str = s.replace('我', '你');

  System.out.println(str); //你是程序员,你在学java

  s = "Welcome to Java World!";

  str = "  sun java    ";

  System.out.println(s.startsWith("Welcome")); //true

  System.out.println(s.endsWith("World")); //false

  System.out.println(s.toLowerCase()); //把字符串全改为小写  welcome to java world!

  System.out.println(s.toUpperCase()); //把字符串全改为大写 WELCOME TO JAVA WORLD!

  String subs = s.substring(11);

  System.out.println(subs); //Java World!

  String sp = str.trim();

  System.out.println(sp); //sum java

  int j = 1234567;

  String sNumber = String.valueOf(j);

  System.out.println("j 是" + sNumber.length() + "位数");

  s = "Mary,F,1985";

  String[] split = s.split(",");

  for ( String str1 : split) {

  System.out.println(str1);

  }

  str = "ab cdGF;D   ";

  countString(str);

  String src = "jav attja vattt aj";

  String sub = " a";

  count(src, sub);

  }

  }


如果觉得 Java常用类(String类)的简单用法这篇文章不错,可以推荐给朋友分享哦。
本文Tags:eda技术,eda技术实用教程,EDA/PLD,
相关EDA/PLD资料


温馨提示; 本站的资料全部免费下载,为方便下次找到本站记得将本站加入收藏夹哦,牢记网址http://www.dianzi6.com

此页提供Java常用类(String类)的简单用法eda技术,eda技术实用教程, EDA/PLD参考。本站还有更多的EDA/PLD相关资料分享。
Copyright© www.dianzi6.com Inc. All rights reserved 。 1 2 3 4 5 6 7 8