Java String类基本操作以及字符串转化为单个字符

String类的基本方法

String​(char[] value) 分配一个新的 String以便它表示当前包含在字符数组参数中的字符序列。(String类的构造方法其实很多,这里简单提一下)
char[] c = {‘I’,’ ', ‘L’, ‘O’, ‘V’, ‘E’, ’ ', ‘J’, ‘A’, ‘V’, ‘A’};
String c1 = new String( c );
String c2 = new String(c, 2, 5);
System.out.println(c1);//输出为I LOVE JAVA
System.out.println(c2);//输出为LOVE
char charAt(int index) 用于返回索引处的char值
int length() 用于返回字符串的长度。
int indexOf​(String str) 返回指定子字符串第一次出现的字符串内的索引.
boolean equals​(Object anObject) 将此字符串与指定对象进行比较。
boolean equalsIgnoreCase​(String anotherString) 将此字符串与另一字符时比较时可忽略大小写。
boolean endsWith​(String suffix) 判断该字符串是否是指定的字符串结尾。
boolean startsWith(String suffix) 判断该字符串是否是指定的字符串开头。
String trim() 使该字符串的开头和结尾的空格删除。
String toLowerCase​(Locale locale) 使该字符串的英文字母全部小写。
String toUpperCase​(Locale locale) 使该字符串的英文字母全部大写。
String replace​(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 oldChar ,在这个字符串 newChar 。
String[] split​(String regex) 将此字符串分割成有若干字符串的数组,例子如下:
String s1 = “hh:ff:dd”;
String[] s2= s1.split(" : ");
for(int i = 0; i<s6.length; i++)
System.out.print(s6[i]);//hh ff dd
String[] split​(String regex,int limit) 将该字符串分割成limit个字符串(不一定有limit个,需要看regex)
String substring​(int beginIndex) 返回值为该字符串的子字符串(子字符串是该字符串从下标beginIndex到结束的字符串)例:
String s1 = “我是程序员, 我爱学JaVA”;
String s2 = new String(s1.substring(6));//返回就是 我爱学JaVA
String substring​(int beginIndex,int endIndex) 返回值为该字符串的子字符串(子字符串是该字符串从下标beginIndex到endIndex的字符串)例:
String s1 = “我是程序员, 我爱学JaVA”;
String s2 = new String(s1.substring(0,4));//返回值就是 我是程序员

static String valueOf​(int i) 返回整型i的字符串表达形式。例:
int t = 123456789;
String s7 = String.valueOf(t);
System.out.println(s7);

以下为巩固String类的基本使用,
1.写了一个求一段字符串中某一个单词的个数。

public class T_String {
  String s10 = "javadjava jnfjsrnfjavajafhusjava";
  int count = 0;
  String s12 = "java";
  while(s10.indexOf(s12)!=-1) {
   int mark = s10.indexOf(s12);
   s10 = new String(s10.substring(mark+s12.length()));
   count++;
  }
  System.out.println(count);
 }

2.求某段字符串中英文字母的个数和数字的个数以及其他字符的个数。

public class T_String {
public static void main(String[] args) {
String s = "dsajfnan7349q8edjsa*&%$ADDJKBF";
 int y=0 ,sz = 0 , ot = 0;
 for(int i = 0; i<s.length(); i++) {
 char s11 = s.charAt(i);
 if(s11>='a'&& s11<='z') {    //这里我其实没写大写字母的个数,只要把a和z换成A和Z就可以求大写字母的个数了。
   y++;
  }
  else if(s11>='0' && s11<='9') {
    sz++;
 }else {
   ot++;
}
}
System.out.println("y="+y+"sz="+sz+"ot="+ot);
}
}

如何让一段字符串分解成单个字符,主要是toCharArray()方法

package com.rong.jiajia;

public class StringTest {

    String str = "ilovejavaforever";

    public void strOperate(String str){
        char[] chars = str.toCharArray();
        for (int i = 0; i <chars.length ; i++) {
            System.out.print(chars[i]+"\t");
        }
    }

    public static void main(String[] args) {
        StringTest st = new StringTest();
        st.strOperate(st.str);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值