浪潮优派培训java笔记:第7章 String

第7章 String

7.1 基本数据类型

String不可以被继承,因为java.lang.String是一个final类。(面试常考)

 

public class TestString{

    public static void main(String[]args) {     

       String s1="Hello";

       String s2="Hello";

       System.out.println(s1==s2);  //true;s1、s2都指向常量区的字符串常量"Hello"

       String s3=new String("hello");

       String s4=new String("hello");

        System.out.println(s3==s4);  //false;s3、s4指向不同的String对象

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

       System.out.println(s3.equals(s4));//true;比较的是所指对象的值而非地址

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

    }

}

 

String是值传递,StringBuffer是引用传递

public class J_Test_4_11 {

    public static void mb_method(Strings, StringBuffer t) {

       s =s.replace('j', 'i');

       t = t.append("C");

    }

    public static void main(String[]args) {

       String a = new String("java");

       StringBufferb = new StringBuffer("java");

       mb_method(a,b);

       System.out.println("a:" + a + " , b:" + b);

    }

}

【运行结果】a:java, b:javaC

public class J_Test_4_13 {

    public static voidmb_method(StringBuffer x, StringBuffer y) {

       x.append(y);

       y = x;  //把x的地址赋给y

    }

    public static void main(String[]args) {

       StringBuffera = new StringBuffer("A");

       StringBufferb = new StringBuffer("B");

       mb_method(a,b);

       System.out.println(a + "," + b);

    }

}

【运行结果】AB,B

 

 

      String类的常用方法(详情参见帮助文档)

  int length();//获取长度

  char charAt(int);//取得指定位置上的字符

  char[] toCharArray();//把指定字符串转换为字符数组

  byte[] getBytes(); //转换为字节数组

  int compareTo(String);//与另一个字符串按字典顺序比较大小,返回值为>0, =0, <0 三种情况之一

  boolean startsWith(String);//判断是否以指定的字符串打头

  boolean endsWith(String);//判断是否以指定的字符串结尾

  String concat(String);//返回与另一个字符串连接后的字符串

  String substring(intbegin);//可用于字符串截取
String substring(int begin,int end);//返回子串,注意不包括end位置上的字符

  String trim();//返回去除两端空格后的字符串

  String replace(char oldc,char newc);
String replaceAll(String olds,String news);//返回替换后的字符串

  String toLowerCase();//转换为小写

 StringtoUpperCase(); //转换为大写

  int indexOf(char/String);
int indexOf(char/String,int offset);//返回字符/串首次出现的位置,无时返回-1

  int lastIndexOf(char/String);
int lastIndexOf(char/String,int offset);//返回字符/串末次出现的位置,无时返回-1

  static String valueOf(xxx);//返回xxx的字符串表示,xxx一般取基本数据类型和字符数组
                     //使用它可以实现数字到字符串的转换

       【String类的常用方法练习】

  字符串t中的字符为( )

    String s = "hypertext";

    String t = s.substring(2, 5);

    System.out.println(t);

   A. "yper "          B. "ype"       C."pert "      D."per"

答案:D  解析:s.substring(2, 5);//从下标为2的取到下标为4的,注意取不到5.

 

输入一个字符串,显示将该字符串中非字母字符删除后的字符串。

public class DeleteStr {

    public static void main(String[]args) {

       String str = "sj$%^&*ASQEkjf_sd/sd[]sd'fds3@s1!";

       String newStr= new String();

       char a[] =str.toCharArray();

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

           System.out.print(a[i] + " ");

           if (a[i] >= 'a' && a[i]<= 'z' || a[i] >= 'A' && a[i] <= 'Z')

              newStr= newStr.concat(String.valueOf(a[i]));

       }

       System.out.println();

       System.out.println("删除后的字符串为:" + newStr);

    }

}

【方法提炼】

1.      将其它类型(如字符串)转换成字符串形式:String.valueOf(char c);

 

 编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数。

方法一:

public class CountString {

    public static void main(String[]args) {

       String s = "I love China!Because I'm a Chinese!";

       int k = 0, m = 0, n =0;

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

           char c = s.charAt(i); //把字符串中的每一个字符赋给字符数组

           if (Character.isUpperCase(c)) //判断是否是大写字母(利用包装类的方法)

              k++;

           else if (Character.isLowerCase(c))

              m++;

           else

              n++;

       }

       System.out.println("大写字母个数有:" + k + "个");

       System.out.println("小写字母个数有:" + m + "个");

       System.out.println("非字母个数有:" + n + "个");

    }

}

方法二:

public class CountString {

    public static void main(String[]args) {

       String s = "I love China!Because I'm a Chinese!";

       int k = 0, m = 0, n =0;

       char c[] = s.toCharArray();

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

           if (c[i] >= 65&& c[i] <= 90)

              k++;

           else if (c[i] >= 97&& c[i] <= 122)

              m++;

           else

              n++;

       }

       System.out.println("大写字母个数有:" + k + "个");

       System.out.println("小写字母个数有:" + m + "个");

       System.out.println("非字母个数有:" + n + "个");

    }

}

编写一个程序,输出在一个字符串中,指定字符串出现的次数。

public class FindStrCount {

    public static void main(String[] args) {

       String str = "I love China!Because I'm a Chinese!";

       String subStr = "Ch";

       int count = 0;

       int index = -1;

       while ((index = str.indexOf(subStr)) != -1) {

           str = str.substring(index +subStr.length());

           System.out.println(str);

           count++;

       }

       System.out.println(count);

    }

}

将取出的字段名转化为变量名,例如: COMPANY_CODE==>companyCode

COMPANY_C->companyC*/

public class ChangeStr {

    public static void main(String[]args) {

       String str = "COMPANY_CODE_NAME";

       String newStr= new String();

       String subStr[] = str.split("_");

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

           subStr[i]= subStr[i].toLowerCase();

           if (i != 0) {

              subStr[i]= subStr[i].substring(0,1).toUpperCase()

                     +subStr[i].substring(1);

           }

           newStr +=subStr[i];

       }

       System.out.println(newStr);

    }

}

【方法提炼】

1.字符串首字母大写:str=str.substring(0,1).toUpperCase();

2.去掉字符串的首字母:str=str.substring(1);

3.以下划线为界分割字符串并赋给一个字符串数组:String subStr[] = str.split("_");

 

编写一个方法,返回一个double型二维数组,数组中的元素通过解析字符串参数获得。如字符串参数:

 "1,2;3,4,5;6,7,8"

 对应的数组为: d[0,0]=1.0  d[0,1]=2.0 

d[1,0]=3.0  d[1,1]=4.0 d[1,2]=5.0

 d[2,0]=6.0 d[2,1]=7.0  d[2,2]=8.0

public class DoubleStr {

    public static void main(String[]args) {

       String str = "1,2;3,4,5;6,7,8";

       StringsubStr[] = str.split(";");

       double d[][] = new double[subStr.length][];

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

           StringsubStr2[] = subStr[i].split(",");

           d[i] = new double[subStr2.length]; //要写在for内循环外面(难点)

           for (int j = 0; j <subStr2.length; j++) {

              d[i][j]= Double.parseDouble(subStr2[j]);

           }

       }

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

           for (int j = 0; j <d[i].length; j++) {

              System.out.print(d[i][j] + " ");

           }

           System.out.println();

       }

    }

}

【方法提炼】

1.       把字符串转换成数值类型(包装类的静态方法):

double d= Double.parseDouble(str);

 

从输入的一行字符串中求出其中最长的英文单词的长度及其个数,并输出。单词之间只能用空格隔开。

 【运行示例】

 请输入以空格隔开的字符串: Nice tomeet you↙

 字符串 = Nice to meet you

 最长单词长度: 4 ,最长单词个数: 2

 【编程提示】输入整行字符串,包括空格符,可以使用Scanner类的nextLine( )方法。*/

import java.util.Scanner;

public class Test {

    public static void main(String[]args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("请输入一行以空格隔开的字符串:");

       String str =sc.nextLine();

       StringsubStr[] = str.split(" ");

       int max = 0, count =0;

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

           if (max <subStr[i].length())

              max =subStr[i].length();

       }

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

           if (max == subStr[i].length())

              count++;

       }

       System.out.println("最长单词长度:" + max + ",最长单词个数:" + count);

    }

}

求一个字符串中字母出现次数(不算空格)

【运行示例】

字符串 = "The Great Wall of China"

运行结果:T-1 h-2 e-2 G-1 r-1 a-3 t-1 W-1 l-2 o-1 f-1C-1 i-1 n-1

public class CountChar {

    public static void main(String[]args) {

       String str = "The Great Wall of China";

       char sub[] =str.toCharArray();

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

           int count = 1; //注意对于每一个字母初始化一次count

           for (int j = i + 1; j <sub.length; j++) {

              if (sub[i] == sub[j]){

                  sub[j]= ' ';

                  count++;

              }

              if (sub[i] != ' ' && j == sub.length - 1) //最后一次比较才输出

                  System.out.print(sub[i] + "-" + count + " ");

           }

       }

    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值