java截取字符串后几位(java程序员必备的基础知识)

2022-07-18,,,,

字符串是引用类型,但是为什么不用new,因为太常用了,就简化了。

如果你不觉得烦,也能写成:

string name = new string("name");
string name = "name"; //就行了

既然是个对象就有属性和方法

它的方法无非就是帮助我们方便的处理这个字符串。

注:使用string一定要注意,必须用一个新的string接受。

string substring = name.substring(1, 3);

(1)符串查找

string 类的 indexof() 方法在字符串中查找子字符串出现的位置, 如果存在返回字符串出现的位置(第一位为0),如果不存在返回 -1。

public class searchstringemp {
        public static void main(string[] args) { 
                string strorig = "xinzhi bigdata java";
                int intindex = strorig.indexof("java"); 
                if(intindex == - 1){
                				system.out.println("没有找到字符串 java"); 
                }else{
                				system.out.println("java 字符串位置 " + intindex);
                }
        }
}

也可以用contains() 方法

(2)字符串替换

java string 类的replace 方法可以替换字符串中的字符。

public class test {
        public static void main(string args[]){ string str="hello world,hello java." ;
                system.out.println(str.replace('h','w')); //替换全部
                system.out.println(str.replacefirst("he","wa")); // 替换第一个遇到的
                system.out.println(str.replaceall("he", "ha")); //替换全部
        }
}

java程序员必备的基础知识_java面向对象之string关键字

(3)字符串分割

split(string) 方法通过指定分隔符将字符串分割为数组。

public class test {
        public static void main(string args[]){ string str="www-baidu-com";
                string delimeter = "-"; //指定分隔符 string[] temp = str.split(delimeter);
                //分割字符串
                //普通for循环
                for(int i =0; i < temp.length; i++){
                        system.out.println(temp[i]);
                        system.out.println("");
                }
                system.out.println("----java for each循 环输出的方法-----");
                string str1 = "www.baidu.com";
                string delimeter1 = "\\."; //指定分隔
                符, .号需要转义,不会明天讲
                string[] temp1 = str1.split(delimeter1); 
                for (string x : temp1){
                        system.out.println(x);
                        system.out.println("");
                }
        }
}

(4)字符串截串

substring(string) 方法可以截取从第几个下标(0开始,包含第一个 开始)到第几个下标(不包含)的字符串。

public class test {
        public static void main(string args[]){
        				name = new string("name"); 
          			substring = name.substring(1, 3);
        }
}

(5)字符串小写转大写

string touppercase() 方法将字符串从小写转为大写。

作业:

查找某个单词在文章中出现的次数:

public static void main(string[] args) { 
  			string str = "hello world abc hello";
        // 截取字符串 第一个包含的 第二个不包含
        test2 test2 = new test2();
        int count = test2.wordcount(str, "hello"); 
        system.out.println(count);
}
public int wordcount(string article, string word){
        //1、先把文章打散成数组
        string[] words = article.split(" ");
        int res = 0;
        for (int i = 0; i < words.length; i++) {
                if(words[i].equalsignorecase(word)){ 
                				res++;
                }
        }
        return res;
}

《java截取字符串后几位(java程序员必备的基础知识).doc》

下载本文的Word格式文档,以方便收藏与打印。