java replace字符替换函数的使用方法

java replace字符替换函数的使用方法

replace(char oldChar, char newChar)

public class MainClass
{
   public static void main( String args[] )
   {
      String s1 = new String( "hello" );
      String s2 = new String( "GOODBYE" );
      String s3 = new String( "   spaces   " );

      System.out.printf( "s1 = %sns2 = %sns3 = %snn", s1, s2, s3 );

      // test method replace     
      System.out.printf("Replace 'l' with 'L' in s1: %snn", s1.replace( 'l', 'L' ) );

   } // end main
}

结果

s1 = hello
s2 = GOODBYE
s3 =    spaces  

Replace 'l' with 'L' in s1: heLLo

将空格换成 /

public class MainClass {

  public static void main(String[] arg) {
    String text = "To be or not to be, that is the question.";
    String newText = text.replace(' ', '/');     // Modify the string text
   
    System.out.println(newText);
  }

}

结果
To/be/or/not/to/be,/that/is/the/question.

有朋友自己写了一个replace函数

// 替换字符串函数
 // String strSource - 源字符串
 // String strFrom  - 要替换的子串
 // String strTo   - 替换为的字符串
 public static String replace(String strSource, String strFrom, String strTo)
 {
   // 如果要替换的子串为空,则直接返回源串
   if(strFrom == null || strFrom.equals(""))
     return strSource;
   String strDest = "";
   // 要替换的子串长度
   int intFromLen = strFrom.length();
   int intPos;
   // 循环替换字符串
   while((intPos = strSource.indexOf(strFrom)) != -1)
   {
     // 获取匹配字符串的左边子串
     strDest = strDest + strSource.substring(0,intPos);
     // 加上替换后的子串
     strDest = strDest + strTo;
     // 修改源串为匹配子串后的子串
     strSource = strSource.substring(intPos + intFromLen);
   }
   // 加上没有匹配的子串
   strDest = strDest + strSource;
   // 返回
   return strDest;
 }

时间: 2024-08-04 02:10:55

java replace字符替换函数的使用方法的相关文章

js replace字符替换函数

今天我们讲到了js的字符替换函数replace详解,主要会讲到他替换简单的字符以及利用正则表达式来处理字符替换,有需的朋友可以看看. 最简单替换函数   <script language="网页特效"> var strm = "javascript is a good script language"; //在此我想将字母a替换成字母a alert(strm.replace("a","a")); </scri

javascript string.replace()字符替换函数

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement)参数 描述 regexp/substr 必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象.   replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数.   11

C++ string.replace()字符替换函数的用法

C++ replace()函数返回string 能放的最大元素个数.(不同于capacity) size _ type max _ size( ) const;   basic_string <char>::size_type cap, max;   cap = s.capacity ( );   max = s.max_size ( ); // max=4294967294.   basic_string::rfind  寻找给定的string.返回找到的第一个string 下标值:如果没找

js 字符替换函数

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-

php字符串与字符替换函数

在php教程替换字符效率最高也是最简单字符替换函数str_replace($arr1,$arr2,$str) 实例一  代码如下 复制代码 str_replace("iwind", "kiki", "i love iwind, iwind said"); 将输出 "i love kiki, kiki said" 结果 即将 原字符串中的所有"iwind"都替换成了"kiki".str_r

js中字符替换函数String.replace()使用技巧_javascript技巧

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement)参数 描述 regexp/substr 必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象.   replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数.

replace MYSQL字符替换函数sql语句分享(正则判断)_Mysql

复制代码 代码如下: Update dede_addonsoft SET dxylink=REPLACE(dxylink, '.zip', '.rar') where aid > 45553; 复制代码 代码如下: update `table_name` set field = replace(field,'.rar','.7z'); table_name:要查询的表名, field:表里的字段名, replace(field,'.rar','.7z'); :正则匹配,把field字段里的 .r

js字符替换函数的详细使用方法

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement) 参数 描述 regexp/substr  必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数.

javascript中replace()字符替换详解

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement) 返回值 一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的. 说明 字符串 stringObject 的 replace() 方法执行的是查找并替换的操作.它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 repl