| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:各位DX帮帮忙啊~~新手请指教
只看楼主 加入收藏
xia_ning
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2010-8-4
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
各位DX帮帮忙啊~~新手请指教
小弟最近在学习C#语言 遇到了一个这样的题目 让小弟把"abc hello bcd" 变成"abc landy bcd"结果 运行结果总是不正确
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace practise
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abc hello bcd";
            char[] chars = str.ToCharArray();
            int index = str.IndexOf("hello");
                Console.WriteLine(index);
                Console.ReadLine();

                if (index != -1)
                {

                    chars[index] = 'l';
                    chars[index++] = 'a';
                    chars[index++] = 'n';
                    chars[index++] = 'd';
                    chars[index++] = 'y';
                    str = new string(chars);
                    Console.WriteLine(str);
                    Console.ReadLine();
                }                                                                  
        }
    }
}}结果为"abc andyo bcd" 小弟 怎么也想不通 还请各位DX帮帮忙啊
搜索更多相关主题的帖子: 指教 
2010-08-04 17:03
dearwolf4128
Rank: 5Rank: 5
来 自:陕西西安
等 级:职业侠客
威 望:6
帖 子:79
专家分:365
注 册:2010-7-2
收藏
得分:15 
你仔细查看C#语言的字符串函数,有一个replace()函数,可以直接实现你的功能,不用你那么麻烦,多看看文档:
REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' )
 

参数
' string_expression1 '
要搜索的字符串表达式。string_expression1 参数的数据类型可以是可隐式转换为 nvarchar 或 ntext 的数据类型。

' string_expression2 '
尝试查找的字符串表达式。string_expression2 参数的数据类型可以是可隐式转换为 nvarchar 或 ntext 的数据类型。

' string_expression3 '
替换字符串表达式。string_expression3 参数的数据类型可以是可隐式转换为 nvarchar 或 ntext 的数据类型。

返回值
nvarchar 或 ntext

如果任意参数为 NULL,则返回 NULL。

示例
下面的示例在产品名称列表中搜索名称“Anton”并将其替换为“Anthony”:

 复制代码
SELECT REPLACE(ProductName, 'Anton', 'Anthony')FROM Products
 
2010-08-04 17:44
saitor
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:5
帖 子:373
专家分:1520
注 册:2009-5-18
收藏
得分:5 
chars[index]=>chars[index+1]
2010-08-04 20:00
快速回复:各位DX帮帮忙啊~~新手请指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017636 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved