有没有其他方式写下面的内容?
string input;
var match = Regex.Match(input, @"Type1");
if (!match.Success)
{
match = Regex.Match(input, @"Type2");
}
if (!match.Success)
{
match = Regex.Match(input, @"Type3");
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想通过一系列表达式运行我的字符串,看看哪一个坚持.
我正在通过这个ASP MVC教程.本教程的这一页面涉及编写一个简单的"搜索"页面.控制器包含此方法:
public ActionResult SearchIndex(string searchString)
{
var movies = from m in db.Movies
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(movies);
}
Run Code Online (Sandbox Code Playgroud)
根据MSDN,String.Contains区分大小写.但是当我导航到时[website url]/Movies/SearchIndex?searchString=mel,它会返回一个带有标题的电影Melancholia.如果我检查控制器方法在调试器,searchString是mel(小写)如预期.
为什么String.Contains这个标题不区分大小写?
我有一个套接字,我写了一些字符数据和一些原始字节数据.对于角色数据,使用a更容易PrintWriter.对于原始字节数据,更容易直接写入OutputStream.所以在我的代码中,我有这样的段:
Writer writer = new PrintWriter(outputStream);
writer.write(someText);
...
writer.flush();
// No call to writer.close(), because that would close the underlying stream.
Run Code Online (Sandbox Code Playgroud)
只要我Writer在开始以其他方式写入流后不小心写这个,这很好.但是我宁愿安全地知道IOException如果我不小心写了一个流,我会得到一个(就像我关闭它一样).
有没有办法明确地防止将来写入a Writer而不关闭其底层流?
根据MSDN,async并await没有创建新线程:
在
async和await关键字不会导致创建额外的线程.
考虑到这一点,我很难理解一些简单程序的控制流程.我的完整示例如下.请注意,它需要Dataflow库,您可以从NuGet安装它.
using System;
using System.Threading.Tasks.Dataflow;
namespace TaskSandbox
{
class Program
{
static void Main(string[] args)
{
BufferBlock<int> bufferBlock = new BufferBlock<int>();
Consume(bufferBlock);
Produce(bufferBlock);
Console.ReadLine();
}
static bool touched;
static void Produce(ITargetBlock<int> target)
{
for (int i = 0; i < 5; i++)
{
Console.Error.WriteLine("Producing " + i);
target.Post(i);
Console.Error.WriteLine("Performing intensive computation");
touched = false;
for (int j = 0; j < 100000000; j++)
;
Console.Error.WriteLine("Finished intensive computation. …Run Code Online (Sandbox Code Playgroud) 我在C#5.0中阅读Async,编译器转换部分包含以下代码段:
public Task<int> AlexsMethod()
{
<AlexsMethod>d__0 stateMachine = new <AlexsMethod>d__0();
stateMachine.<>4__this = this;
stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start<<AlexsMethod>d__0>(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
Run Code Online (Sandbox Code Playgroud)
有两段符号对我来说是新的.首先是<AlexsMethod>d__0.第二是stateMachine.<>4__this.当我自己尝试时它们都不起作用,所以我怀疑它只供编译器使用.但是我在搜索有关这种符号的意图的更多信息时遇到了麻烦.
我想将鼠标悬停在我的图像上,并将文本显示在图像的位置,但我不想使用jQuery或JavaScript.
#wrapper .text {
position: relative;
bottom: 30px;
left: 0px;
visibility: hidden;
}
#wrapper:hover .text {
visibility: visible;
}Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
<img src="kero.png" class="hover" height="200px" width="200px/>
<p class="text">text</p>
</div>?Run Code Online (Sandbox Code Playgroud)
在C++中,void somefunction(int)传递一个值,同时void somefunction(int&)传递一个引用.在Java中,基元通过值传递,而对象通过引用传递.python是如何做出这个决定的?
编辑:因为所有内容都是通过引用传递的,为什么会这样:
def foo(num):
num *= 2
a = 4
foo(a)
print(a)
Run Code Online (Sandbox Code Playgroud)
打印'4'而不是'8'?
在C#中,你可以这样做:
SomeClass someClass = new SomeClass () {
SomeProperty = someValue
};
Run Code Online (Sandbox Code Playgroud)
这个语法叫什么?
我想制作一个重启tomcat6的目标.现在,我有这样的事情:
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask" />
<taskdef name="start" classname="org.apache.catalina.ant.StartTask" />
...
<target name="restart" depends="deploy" description="Restart Tomcat" >
<stop url="${manager}" username="${username}" password="${password}" path="${path}" />
<start url="${manager}" username="${username}" password="${password}" path="${path}" />
</target>
Run Code Online (Sandbox Code Playgroud)
我可以在开始前依赖停止运行吗?或者我应该制作两个单独的目标,并且"开始"依赖于"停止"?
这两个问题为洗刷IEnumerable提供了类似的算法:
以下是两种方法并排:
public static IEnumerable<T> Shuffle1<T> (this IEnumerable<T> source)
{
Random random = new Random ();
T [] copy = source.ToArray ();
for (int i = copy.Length - 1; i >= 0; i--) {
int index = random.Next (i + 1);
yield return copy [index];
copy [index] = copy [i];
}
}
public static IEnumerable<T> Shuffle2<T> (this IEnumerable<T> source)
{
Random random = new Random ();
List<T> copy = source.ToList ();
while (copy.Count > 0) {
int index …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×3
async-await ×2
java ×2
linq ×2
syntax ×2
ant ×1
asp.net-mvc ×1
css ×1
html ×1
ienumerable ×1
io ×1
notation ×1
outputstream ×1
performance ×1
pointers ×1
python ×1
reference ×1
regex ×1
sockets ×1
tomcat ×1