Redian新闻
>
问一个吵架笨笨,菩萨怕怕stupid C#问题
avatar
问一个吵架笨笨,菩萨怕怕stupid C#问题# DotNet - 窗口里的风景
p*p
1
在C/c++ 里面要让一个char从a变成d, 可以这样写
char tmp='a';
tmp+=3;
但是C#里面char和整型不一样了,怎么做同样的事情呢???
高手给我一点帮助吧,谢谢了
avatar
L*r
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和整型不一样了,怎么做同样的事情呢???
: 高手给我一点帮助吧,谢谢了

avatar
L*r
3
有点overkill了吧
using unsafe code for this problem ... :)

statement
avatar
p*p
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.

avatar
L*r
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

avatar
L*r
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 :)

avatar
L*r
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.
:
: 了

avatar
w*n
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);
:

avatar
L*r
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 ? :)
avatar
p*p
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

avatar
w*n
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

avatar
L*r
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

avatar
w*n
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. :)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。