我自己的替换版本 [英] My own version of replace

查看:80
本文介绍了我自己的替换版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我尝试编写自己的替换版本,但它不起作用。我希望

有人可以帮帮我。

它只取代第一个字母!


为什么我不能使用public string.replace函数?好吧我喜欢

实现字符串函数:-)。


我编写了另一个替换单词中的字符的函数这个

功能有效!


感谢您的帮助


使用:


string text =" ;早安" ;;

replaceStr(文字,早晨,晚上);

Console.WriteLine(文字);

输出:Good Eorning //只有第一个字母


这是函数


public static void replaceStr(string word ,字符串word2,字符串

替换)

{

int i;

int x = 0,y, z = 0,l = 0;

char [] place = new char [word.Length];

for(i = 0; i< word.Length; i ++)

{

place [x] = word [i]; //填充地点数组

x ++;

}


for(y = 0; y< place.Length; y ++)

{


if(place [y] == word2 [z])/

{


place [y] = replace [l]; //它只取代

第一个字母

l ++;

y ++;

z ++;


}

}


Console.WriteLine(地方);


}

Hi all!

I try to code my own version of replace but it doesn''t work. I hope
somebody can help me out.
It only replaces the first char!.

Why I don''t use the public string.replace function? Well I love
implementing string functions :-).

I coded another function for replacing chars in a word and this
function works!

Thank you for any help

Use:

string text = "Good Morning";
replaceStr(text,"Morning","Evening");
Console.WriteLine(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(string word, string word2, string
replace)
{
int i;
int x = 0, y, z = 0, l = 0;
char[] place = new char[word.Length];
for (i = 0; i < word.Length; i++)
{
place[x] = word[i]; // fills the place Array
x++;
}

for (y = 0; y < place.Length; y++)
{

if (place[y] == word2[z]) /
{

place[y] = replace[l]; // It only replaces the
first char
l++;
y++;
z++;

}
}

Console.WriteLine(place);

}

推荐答案

11月29日17:08,实现< tomico ... @ gmail.comwrote:
On 29 Nov, 17:08, implement <tomico...@gmail.comwrote:

大家好!


我尝试编写自己的替换版本,但它不起作用。我希望

有人可以帮帮我。

它只取代第一个字母!


为什么我不能使用public string.replace函数?好吧我喜欢

实现字符串函数:-)。


我编写了另一个替换单词中的字符的函数这个

功能有效!


感谢您的帮助


使用:


string text =" ;早安" ;;

replaceStr(文字,早晨,晚上);

Console.WriteLine(文字);

输出:Good Eorning //只有第一个字母


这是函数


public static void replaceStr(string word ,字符串word2,字符串

替换)

{

int i;

int x = 0,y, z = 0,l = 0;

char [] place = new char [word.Length];

for(i = 0; i< word.Length; i ++)

{

place [x] = word [i]; //填充地点数组

x ++;

}


for(y = 0; y< place.Length; y ++)

{


if(place [y] == word2 [z])/

{


place [y] = replace [l]; //它只取代

第一个字母

l ++;

y ++;

z ++;


}


}


Console.WriteLine(地方);


}
Hi all!

I try to code my own version of replace but it doesn''t work. I hope
somebody can help me out.
It only replaces the first char!.

Why I don''t use the public string.replace function? Well I love
implementing string functions :-).

I coded another function for replacing chars in a word and this
function works!

Thank you for any help

Use:

string text = "Good Morning";
replaceStr(text,"Morning","Evening");
Console.WriteLine(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(string word, string word2, string
replace)
{
int i;
int x = 0, y, z = 0, l = 0;
char[] place = new char[word.Length];
for (i = 0; i < word.Length; i++)
{
place[x] = word[i]; // fills the place Array
x++;
}

for (y = 0; y < place.Length; y++)
{

if (place[y] == word2[z]) /
{

place[y] = replace[l]; // It only replaces the
first char
l++;
y++;
z++;

}

}

Console.WriteLine(place);

}



你已经加倍了y。当你尝试使用

replaceStr(GoMod Morning,Morning)时,你会发现第二个问题。

,晚上);

You''ve double incremented y. There''s a second issue you''ll find when
you try the test line with

replaceStr("GoMod Morning", "Morning", "Evening");





" implement"写道:


"implement" wrote:

大家好!


我尝试编写自己的替换版本,但它不起作用。我希望

有人可以帮帮我。

它只取代第一个字母!


为什么我不能使用public string.replace函数?好吧我喜欢

实现字符串函数:-)。


我编写了另一个替换单词中的字符的函数这个

功能有效!


感谢您的帮助


使用:


string text =" ;早安" ;;

replaceStr(文字,早晨,晚上);

Console.WriteLine(文字);

输出:Good Eorning //只有第一个字母


这是函数


public static void replaceStr(string word ,字符串word2,字符串

替换)

{

int i;

int x = 0,y, z = 0,l = 0;

char [] place = new char [word.Length];

for(i = 0; i< word.Length; i ++)

{

place [x] = word [i]; //填充地点数组

x ++;

}


for(y = 0; y< place.Length; y ++)

{


if(place [y] == word2 [z])/

{


place [y] = replace [l]; //它只取代

第一个字母

l ++;

y ++;

z ++;


}


}


Console.WriteLine(地方);


}
Hi all!

I try to code my own version of replace but it doesn''t work. I hope
somebody can help me out.
It only replaces the first char!.

Why I don''t use the public string.replace function? Well I love
implementing string functions :-).

I coded another function for replacing chars in a word and this
function works!

Thank you for any help

Use:

string text = "Good Morning";
replaceStr(text,"Morning","Evening");
Console.WriteLine(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(string word, string word2, string
replace)
{
int i;
int x = 0, y, z = 0, l = 0;
char[] place = new char[word.Length];
for (i = 0; i < word.Length; i++)
{
place[x] = word[i]; // fills the place Array
x++;
}

for (y = 0; y < place.Length; y++)
{

if (place[y] == word2[z]) /
{

place[y] = replace[l]; // It only replaces the
first char
l++;
y++;
z++;

}
}

Console.WriteLine(place);

}



我认为你会遇到比这更多的问题,但是,你不应该给你带来更多的问题(是的)在你的if语句中。你最终错过了一个

字符。


更改后,尝试运行以下代码:


string text =" Guten Morgen";

replaceStr(text,Morning,Abend);

Console.WriteLine(text);


在完全检查它是否等于

早上之前你开始替换Morgen这个词。

I think you are going to have more problems than this, but, you should not
be incrimenting y (y++) inside your if statement. You end up missing a
character.

After the change, try running your code on the following:

string text = "Guten Morgen";
replaceStr(text,"Morning","Abend");
Console.WriteLine(text);

You start to replace the word Morgen before completely checking if it equals
Morning.


11月29,上午11:08,实现< tomico ... @ gmail.comwrote:
On Nov 29, 11:08 am, implement <tomico...@gmail.comwrote:

大家好!


我尝试编写自己的替换版本,但它不起作用。我希望

有人可以帮帮我。

它只取代第一个字母!


为什么我不能使用public string.replace函数?好吧我喜欢

实现字符串函数:-)。


我编写了另一个替换单词中的字符的函数这个

功能有效!


感谢您的帮助


使用:


string text =" ;早安" ;;

replaceStr(文字,早晨,晚上);

Console.WriteLine(文字);

输出:Good Eorning //只有第一个字母


这是函数


public static void replaceStr(string word ,字符串word2,字符串

替换)

{

int i;

int x = 0,y, z = 0,l = 0;

char [] place = new char [word.Length];

for(i = 0; i< word.Length; i ++)

{

place [x] = word [i]; //填充地点数组

x ++;

}


for(y = 0; y< place.Length; y ++)

{


if(place [y] == word2 [z])/

{


place [y] = replace [l]; //它只取代

第一个字母

l ++;

y ++;

z ++;


}


}


Console.WriteLine(地方);


}
Hi all!

I try to code my own version of replace but it doesn''t work. I hope
somebody can help me out.
It only replaces the first char!.

Why I don''t use the public string.replace function? Well I love
implementing string functions :-).

I coded another function for replacing chars in a word and this
function works!

Thank you for any help

Use:

string text = "Good Morning";
replaceStr(text,"Morning","Evening");
Console.WriteLine(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(string word, string word2, string
replace)
{
int i;
int x = 0, y, z = 0, l = 0;
char[] place = new char[word.Length];
for (i = 0; i < word.Length; i++)
{
place[x] = word[i]; // fills the place Array
x++;
}

for (y = 0; y < place.Length; y++)
{

if (place[y] == word2[z]) /
{

place[y] = replace[l]; // It only replaces the
first char
l++;
y++;
z++;

}

}

Console.WriteLine(place);

}



这是因为y在循环的每次交互中都会递增两次。


即使之后你确定还存在一些问题。首先,它将取代它不应该取代的东西。其次,字符串是
不可变的,所以除非你返回一个新的字符串,该函数基本上只是一个消耗noop的CPU。

That''s because y is incremented twice on each interation of the loop.

Even after you fix that there''s still some problems. First, it will
replace things it should not be replacing. Second, strings are
immutable so unless you return a new string that function is basically
nothing more than a CPU consuming noop.


这篇关于我自己的替换版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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