如何使用c#.net在不使用replace关键字的情况下用其他字符串替换字符串 [英] How to replace a string with other string without replace keyword using c#.net

查看:122
本文介绍了如何使用c#.net在不使用replace关键字的情况下用其他字符串替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string str ="kusuma";

1)代替"u",给定单词必须替换
2)代替"u",给定字母必须替换

string str="kusuma";

1)in place of "u" the given word has to replace
2)In place of "u" the given letter has to replace

推荐答案

string word = "kusuma";
char c = 'u';
string newword=String.Empty;
string new_string = "x";
int i = 0;
do
{
    if (word[i] == c)
        newword += new_string;
    else
        newword += word[i];

    i++;
}
while (i < word.Length);

Console.WriteLine(newword);
Console.ReadLine();


这是家庭作业,我们不为他人做家庭作业.

0)如果不使用Replace,没有人会以程序员为生.要求太可笑了.

1)这是一个非常简单的编程任务,您应该能够在没有任何帮助的情况下执行它.如果您不学习分析问题,那么您将如何学习编程方面的知识?

2)如果您在毫无疑问的短暂职业生涯中会变得如此懒惰,则应该考虑不需要大量大脑活动性的工作,例如将胶水涂在信封盖上.
This is homework, and we don''t do homework for people.

0) Nobody working as a programmer for a living is going to do it without using Replace. The requirement is ludicrous.

1) This is such a simple programming task that you should be able to perform it without any help. If you don''t learn to analyze a problem, how are you going to learn anything with regards to programming?

2) If you''re going to be this lazy throughout your doubtlessly brief career, you should consider a line of work that doesn''t require a lot of cerebral mobility, like applying glue to envelop flaps.




这是几种方法之一

Hi,

this is one of a few methods

string word = "kusuma";
char c = 'u';
string new_string = "x";
while (word.IndexOf(c) != -1)
{
   int index = word.IndexOf(c);
   word = word.Remove(index, 1); // remove one char
   word = word.Insert(index, new_string);
}



希望对您有所帮助:)

戴夫



Hope it helps:)

Dave


这篇关于如何使用c#.net在不使用replace关键字的情况下用其他字符串替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆