p*******p 发帖数: 13670 | 1 在C/c++ 里面要让一个char从a变成d, 可以这样写
char tmp='a';
tmp+=3;
但是C#里面char和整型不一样了,怎么做同样的事情呢???
高手给我一点帮助吧,谢谢了 | L*******r 发帖数: 1011 | 2 just do explicit cast:
char tmp = 'a';
tmp = (char)(tmp + 3);
Console.WriteLine(tmp);
it works.
【在 p*******p 的大作中提到】 : 在C/c++ 里面要让一个char从a变成d, 可以这样写 : char tmp='a'; : tmp+=3; : 但是C#里面char和整型不一样了,怎么做同样的事情呢??? : 高手给我一点帮助吧,谢谢了
| L*******r 发帖数: 1011 | 3 有点overkill了吧
using unsafe code for this problem ... :)
statement | p*******p 发帖数: 13670 | 4 i see, 那么我现在的问题是整个字符串的每个字符都要移动3袼,是不是这样就可以了
string tmp=Console.Readline();
for(i=0;i
{
tmp[i]=(char)(tmp[i]+3);
}
3x
【在 L*******r 的大作中提到】 : just do explicit cast: : char tmp = 'a'; : tmp = (char)(tmp + 3); : Console.WriteLine(tmp); : it works.
| L*******r 发帖数: 1011 | 5 your idea is ok.
but:
1. you can't do tmp[i] = ....., that one is read only.
If you know java, you must know string is inmutable.
2. don't forget the declaration of "i". int i . I know that's trivial :)
【在 p*******p 的大作中提到】 : i see, 那么我现在的问题是整个字符串的每个字符都要移动3袼,是不是这样就可以了 : string tmp=Console.Readline(); : for(i=0;i: { : tmp[i]=(char)(tmp[i]+3); : } : 3x
| L*******r 发帖数: 1011 | 6 So you should create a new string, append every "result" character to that
string. then do tmp = tmp1. hehe. your old string will be recycled by the
system later.
了
【在 L*******r 的大作中提到】 : your idea is ok. : but: : 1. you can't do tmp[i] = ....., that one is read only. : If you know java, you must know string is inmutable. : 2. don't forget the declaration of "i". int i . I know that's trivial :)
| L*******r 发帖数: 1011 | 7 code example:
string tmp1 = "aaaa";
string tmp2 = "";
for(int i=0;i
{
tmp2 += (char)(tmp1[i]+3);
}
tmp1 = tmp2;
Console.WriteLine(tmp1);
以
【在 L*******r 的大作中提到】 : So you should create a new string, append every "result" character to that : string. then do tmp = tmp1. hehe. your old string will be recycled by the : system later. : : 了
| w****n 发帖数: 241 | 8 ft, is this the beauty of o-o stuff ? :)
【在 L*******r 的大作中提到】 : code example: : string tmp1 = "aaaa"; : string tmp2 = ""; : for(int i=0;i: { : tmp2 += (char)(tmp1[i]+3); : } : tmp1 = tmp2; : Console.WriteLine(tmp1); :
| L*******r 发帖数: 1011 | 9 hehe, just the immutable and mutable issue. Very common issue.
When I was teaching Java for cs undergraduate, they always asked those kind of
"string" questions. :)
Anyway, you have stringbuffer in Java.
I talked about stringbuilder, whcih is "stringbuffer" equivalent in C#, in my
recent post. :)
that
the
【在 w****n 的大作中提到】 : ft, is this the beauty of o-o stuff ? :)
| p*******p 发帖数: 13670 | 10 thanks, got u, :)
【在 L*******r 的大作中提到】 : hehe, just the immutable and mutable issue. Very common issue. : When I was teaching Java for cs undergraduate, they always asked those kind of : "string" questions. :) : Anyway, you have stringbuffer in Java. : I talked about stringbuilder, whcih is "stringbuffer" equivalent in C#, in my : recent post. :) : : that : the
| w****n 发帖数: 241 | 11 name is too long, hehe
why not 'strbuf' and 'strbuilder' hehe
【在 L*******r 的大作中提到】 : hehe, just the immutable and mutable issue. Very common issue. : When I was teaching Java for cs undergraduate, they always asked those kind of : "string" questions. :) : Anyway, you have stringbuffer in Java. : I talked about stringbuilder, whcih is "stringbuffer" equivalent in C#, in my : recent post. :) : : that : the
| L*******r 发帖数: 1011 | 12 hehe. In the old C/C++ tutorial, experts tell us to make a resonably long
name.
But actually, it doesn't hurt to write long meaningful name using vi, emacs or
any IDE. Do you really write them character by character? No, typically you
don't.
Does it hurt to read a name like "stringbuffer"? No, I don't think so.
So ... hehe. :)
BTW, 'strbuf' and 'strbuilder'are ok as your own types. But there are many of
beginners, the names of base classes have to be meaningful and damn easy to
understand. :)
k
【在 w****n 的大作中提到】 : name is too long, hehe : why not 'strbuf' and 'strbuilder' hehe
| w****n 发帖数: 241 | 13 em... i'll write _s_b_
then do a full text replace :)
【在 L*******r 的大作中提到】 : hehe. In the old C/C++ tutorial, experts tell us to make a resonably long : name. : But actually, it doesn't hurt to write long meaningful name using vi, emacs or : any IDE. Do you really write them character by character? No, typically you : don't. : Does it hurt to read a name like "stringbuffer"? No, I don't think so. : So ... hehe. :) : BTW, 'strbuf' and 'strbuilder'are ok as your own types. But there are many of : beginners, the names of base classes have to be meaningful and damn easy to : understand. :)
|
|