由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
DotNet版 - 问一个吵架笨笨,菩萨怕怕stupid C#问题
相关主题
stringbuildeRe: 问一个吵架笨笨,菩萨怕怕stupid C#问题对 (im)mutability 的误解和深度理解
求救一个小问题FP更接近人的思维
razor view as email templateGoogle 电面
be aware different behavior of stringbuilder in .net 4.0问一道uber onsite题目
请教 pass by value or reference 的问题问个java String问题
笨笨一问:请问Visual Studios.NET和Visual Studio 2003/2005什么关系啊?发个Twitter的面试题
一道很简单的面试题,但是不知道哪个算法好请教:怎样函数里改变一个Double变量的值?
ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有编程语言有两种,拼关键字的,拼标点的
相关话题的讨论汇总
话题: tmp话题: string话题: char话题: tmp1话题: c#
进入DotNet版参与讨论
1 (共1页)
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. :)

1 (共1页)
进入DotNet版参与讨论
相关主题
编程语言有两种,拼关键字的,拼标点的请教 pass by value or reference 的问题
从今天开始起,学C++!笨笨一问:请问Visual Studios.NET和Visual Studio 2003/2005什么关系啊?
Python Q: function pass in struct pointer, come back with data filled一道很简单的面试题,但是不知道哪个算法好
也谈OOP跟FP之争ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有
stringbuildeRe: 问一个吵架笨笨,菩萨怕怕stupid C#问题对 (im)mutability 的误解和深度理解
求救一个小问题FP更接近人的思维
razor view as email templateGoogle 电面
be aware different behavior of stringbuilder in .net 4.0问一道uber onsite题目
相关话题的讨论汇总
话题: tmp话题: string话题: char话题: tmp1话题: c#