`

String的常用操作方法

阅读更多
                      String的常用操作方法

2.1、字符与字符串
       在String类中提供了以下的方法操作字符与字符串间的转换关系:
|-根据字符串中提供的索引找到指定位置的字符:public char charAt(int index)
|-将字符串变为字符数组:public char[] toCharArray()
|-将字符数组变为字符串:
|-将全部的字符数组变为String类型:public String(char[] value)
|-将部分的字符数组变为String类型:” public String(char[] value,int offset,int count)
范例:取出字符串中制定位置的字符


public class StringAPIDemo01
{
       public static void main(String args[])
       {
              String str = "hello" ;
              char c = str.charAt(2) ;
              System.out.println(c) ;
       }
}



范例:字符串 –-> 字符数组
public class StringAPIDemo02
{
       public static void main(String args[])
       {
              String str = "hello world !!!!!#@" ;
              char c[] = str.toCharArray() ;
              for (int i = 0 ; i < c.length ; i++ )
              {
                     System.out.print(c[i] + "、" ) ; 
              }
              String str1 = new String(c) ;
              String str2 = new String (c,0,5) ;
              System.out.println("\n"+str1) ;
              System.out.println(str2) ;
       }
}


2.2、字节与字符串
       与字符数组的操作一致,一个字符串也可以变为字节数组,一个字节数组也可以变为字符串:
              |-String à字节数组:public byte[] getBytes()
        |-字节数组 àString
            |-全部:public String(byte[] bytes)
            |-部分:public String(byte[] bytes,int offset,int length)
范例:字节数组 à字符串

public class StringAPIDemo03
{
    public static void main(String args[])
    {
        String str = "hello world !!!!!#@" ;
        byte b[] = str.getBytes() ;  //将字符串变为字节数组 
        String str1 = new String(b) ;
        String str2 = new String (b,0,5) ;
        System.out.println("\n"+str1) ;
        System.out.println(str2) ;
    }
}



2.3、判断是否以指定的字符串开头或结尾
       |-判断是否以指定的字符串开头:public boolean startsWith(String prefix)
    |-判断是否以指定的字符串结尾public boolean endsWith(String suffix):
范例:验证操作


public class StringAPIDemo04
{
    public static void main(String args[])
    {
        String str = "**hello world ##" ;
        System.out.println(str.startsWith("**")) ;
        System.out.println(str.endsWith("##")) ;
    }
}




2.4、替换操作
       使用以下的方法可以完成替换的操作:

        |-public String replaceAll(String regex,String replacement)
范例:替换内容


public class StringAPIDemo05
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String newStr = str.replaceAll("l","x") ;
        System.out.println(newStr) ;
    }
}


2.5、字符串的截取
       使用以下两种方法可以完成字符串的截取操作:
              |-全部截取:public String substring(int beginIndex)
        |-部分截取:public String substring(int beginIndex,int endIndex)
范例:验证操作


public class StringAPIDemo06
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String sub1 = str.substring(6) ;   //从第六个字符开始截取 
        String sub2 = str.substring(0,5) ;  //从第一个字符截取到第五个字符 
        System.out.println(sub1) ;
        System.out.println(sub2) ;
    }
}



2.6、字符串的拆分
       可以将字符串按照指定的内容进行拆分操作:
              |-public String[] split(String regex)
范例:拆分字符串

public class StringAPIDemo07
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        String s[] = str.split(" ") ;
        for (String st :s)
        {
            System.out.println(st) ;
        }
    }
}



2.7、字符串查找
       如果需要在一个字符串中查找是否存在指定的内容,可以使用以下的两个方法:
              |-取得指定字符串的位置:public int indexOf(int ch),public int indexOf(int ch, int fromIndex)
            |-此方法返回int型数据,如果查到了则返回位置,查不到,返回-1 ;
        |-直接查找:public boolean contains(String s)
范例:查找操作


public class StringAPIDemo08
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.contains("hello")) ;    //ture
        System.out.println(str.contains("aust")) ;      //false
    }
}




范例:查找位置

public class StringAPIDemo09
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.indexOf("hello")) ;
        System.out.println(str.indexOf("aust")) ;
        if((str.indexOf("aust")) != -1)
        {
            System.out.println("查找到所需的内容。"); 
        }
    }
}



范例:指定查找的开始位置

public class StringAPIDemo10
{
    public static void main(String args[])
    {
        String str = "hello world " ;
        System.out.println(str.indexOf("hello")) ;
        System.out.println(str.indexOf("hello " , 6)) ;
    }
}



2.8、字符串的其他操作
       去掉左右空格:public String trim()
       取的字符串长度:public int length()
       转大写:public String toUpperCase()
       转小写:public String toLowerCase()
范例:

public class StringAPIDemo11
{
    public static void main(String args[])
    {
        String str = " hello world " ;
        System.out.println(str.trim()) ;
        System.out.println(str.trim().length());
        System.out.println(str.trim().toUpperCase()) ;
    }
}
分享到:
评论

相关推荐

    C#中String类常用方法汇总

    主要介绍了C#中String类常用方法,较为详细的汇总了String类中的常用方法,对于深入掌握C#字符串操作有着很好的学习借鉴价值,需要的朋友可以参考下

    string类的常用方法,附带详细操作步骤.txt

    string类的常用方法

    string类的常用方法(具体操作和步骤).txt

    string类的常用方法

    string 类及所有的方法(c++)

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。...

    string类的常用方法.pdf

    string类是C++中一个非常常用的类,它提供了许多方法来操作字符串。以下是一些常用的string类方法: 1. 构造函数: • string(): 创建一个空的字符串。 ◦ string(const char* str): 用一个C风格字符串来初始化一个...

    C++中 String 类的常用方法.md

    string类的常用方法 `String` 类在 C++ 中是一个非常基础和常用的类,用于处理字符串。以下是一些 `string` 类的常用方法: 1. **构造函数** * `string()`: 创建一个空字符串。 * `string(const char* s)`: ...

    js字符串类型String常用操作实例总结

    本文实例讲述了js字符串类型String常用操作。分享给大家供大家参考,具体如下: 字符串是不可变的。 对字符串的所有操作都会返回一个新字符串,原字符串不变 在 ie 6-7 时,’ abc ‘ + ‘ cdf ‘ 如果两个大量的...

    c#常用通用操作方法类

    CommonMethod(通用操作方法类 1 根据列名字符串数组,创建自定义DataTable CreateSelfDataTable(String[] sList) 2 DES加密解密 3 webform后台js弹层 4 winform后台弹层封装 5 常用正则表达式验证

    Java-SE中的String世界.pptx.pptx

    String构造方法概述 String类提供了多种构造方法,用于创建不同类型的字符串对象,...在实际编程中,我们经常使用String构造方法来处理字符串,如拼接字符串、转换大小写等,这些操作都可以通过String构造方法实现。

    JAVA 面向对象程序设计第6章 Java常用类.pptx

    6.2.2 String类常用方法;6.2.2 String类常用方法;6.2.2 String类常用方法;6.2.2 String类常用方法;6.2.2 String类常用方法;6.2.3 StringBuffer类常用方法;6.2.3 StringBuffer类常用方法;6.2.3 StringBuffer类常用...

    js String对象中常用方法小结(字符串操作)

    js String对象中常用方法小结,需要的朋友可以参考下

    文件操作常用方法

    一些文件的常用操作 writeDate(Context context,InputStream is, File file, String charSet) getDataFromAssets(Context context,String path, String charSet) getText(Context context, String path, String ...

    string类的常用方法.zip

    但是,你可以使用 str 类的方法来创建新的字符串或对字符串进行操作。 1. capitalize() 将字符串的首字母大写,其余字母小写。 2. lower() 和 upper() 将字符串转换为小写或大写。 3. strip(), lstrip(), ...

    CString,string,char*之间的转换

    string的c_str()也是非常常用的,但要注意和char *转换时,要把char定义成为const char*,这样是最安全的。 以上函数UNICODE编码也没问题:unicode下照用,加个_T()宏就行了,像这样子_T("%s") 补充: CString ...

    String工具类

    String工具类,包括对String对象的一些常用操作方法。

    深入解析String类:掌握Java中字符串处理的关键方法.zip

    string类的常用方法在Java编程中,String类无疑是我们最常用到的一个类。无论是用户输入、文件读取还是网络传输,字符串操作无处不在。掌握String类的常用方法,对于提高编程效率和代码质量至关重要。本篇博文将带你...

    JavaScript字符串String和Array操作的有趣方法

    字符串和数组在程序编写过程中是十分常用的类型,因此程序语言都会将String和Array作为基本类型,并提供许多字符串和数组的方法来简化对字符串的操作。JavaScript里面也提供了String类型和Array类型,并且有很多基本...

    StringUtil.java

    java字符串操作常用的方法,包括了截取字符串里的图片链接, 从html中提取纯文本,把字符串转换成16进制颜色

Global site tag (gtag.js) - Google Analytics