标签: if-statement

Java String - 查看字符串是否仅包含数字而不包含字母

我有一个字符串,我在我的应用程序中加载,它从数字变为字母等.我有一个简单的if声明,看它是否包含字母或数字,但有些东西不能正常工作.这是一个片段.

String text = "abc"; 
String number; 

if (text.contains("[a-zA-Z]+") == false && text.length() > 2) {
    number = text; 
}
Run Code Online (Sandbox Code Playgroud)

虽然text变量确实包含字母,但条件返回为true.的和&&应作为EVAL两个条件不必是true为了处理number = text;

==============================

解:

我能够通过使用此问题的评论提供的以下代码来解决这个问题.所有其他帖子也有效!

我使用的工作来自第一条评论.虽然提供的所有示例代码似乎也是有效的!

String text = "abc"; 
String number; 

if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2) {
    number = text; 
}
Run Code Online (Sandbox Code Playgroud)

java string if-statement

167
推荐指数
8
解决办法
49万
查看次数

我可以使用if(指针)而不是if(指针!= NULL)吗?

检查指针是不是NULL通过简单写入if(pointer)或我必须使用它是否安全if(pointer != NULL)

c++ null pointers if-statement null-pointer

164
推荐指数
6
解决办法
8万
查看次数

切换if-else语句的优点

使用switch语句与使用if30个unsigned枚举的语句的最佳实践是什么,其中大约10个具有预期的操作(目前是相同的操作).需要考虑性能和空间,但并不重要.我已经抽象了代码片段,所以不要因为命名惯例而讨厌我.

switch 声明:

// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing

switch (numError)
{  
  case ERROR_01 :  // intentional fall-through
  case ERROR_07 :  // intentional fall-through
  case ERROR_0A :  // intentional fall-through
  case ERROR_10 :  // intentional fall-through
  case ERROR_15 :  // intentional fall-through
  case ERROR_16 :  // intentional fall-through
  case ERROR_20 :
  {
     fire_special_event();
  }
  break;

  default:
  { …
Run Code Online (Sandbox Code Playgroud)

c++ optimization if-statement switch-statement

162
推荐指数
9
解决办法
10万
查看次数

在Bash中测试非零长度字符串:[ - n"$ var"]或["$ var"]

我已经看到bash脚本以两种不同的方式测试非零长度字符串.大多数脚本使用-n选项:

#!/bin/bash
# With the -n option
if [ -n "$var" ]; then
  # Do something when var is non-zero length
fi
Run Code Online (Sandbox Code Playgroud)

但是-n选项并不是真正需要的:

# Without the -n option
if [ "$var" ]; then
  # Do something when var is non-zero length
fi
Run Code Online (Sandbox Code Playgroud)

哪种方式更好?

同样,这是测试零长度的更好方法:

if [ -z "$var" ]; then
  # Do something when var is zero-length
fi
Run Code Online (Sandbox Code Playgroud)

要么

if [ ! "$var" ]; then
  # Do something when var is zero-length
fi
Run Code Online (Sandbox Code Playgroud)

syntax bash shell if-statement

160
推荐指数
4
解决办法
12万
查看次数

如何在XSLT中实现if-else语句?

我试图在XSLT中实现if -else语句,但我的代码只是不解析.有没有人有任何想法?

  <xsl:variable name="CreatedDate" select="@createDate"/>
  <xsl:variable name="IDAppendedDate" select="2012-01-01" />
  <b>date: <xsl:value-of select="$CreatedDate"/></b> 

  <xsl:if test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:if>
  <xsl:else>
    <h2> dooooooooooooo </h2>
  </xsl:else>
Run Code Online (Sandbox Code Playgroud)

xml xslt if-statement

159
推荐指数
3
解决办法
40万
查看次数

dplyr包可以用于条件变异吗?

当突变是有条件的(取决于某些列值的值)时,是否可以使用mutate?

这个例子有助于显示我的意思.

structure(list(a = c(1, 3, 4, 6, 3, 2, 5, 1), b = c(1, 3, 4, 
2, 6, 7, 2, 6), c = c(6, 3, 6, 5, 3, 6, 5, 3), d = c(6, 2, 4, 
5, 3, 7, 2, 6), e = c(1, 2, 4, 5, 6, 7, 6, 3), f = c(2, 3, 4, 
2, 2, 7, 5, 2)), .Names = c("a", "b", "c", "d", "e", "f"), row.names = c(NA, 
8L), class = "data.frame")

  a b c …
Run Code Online (Sandbox Code Playgroud)

if-statement r case-when dplyr mutate

157
推荐指数
5
解决办法
15万
查看次数

如何在Bash中检查文件是否为空?

我有一个名为diff.txt的文件.想检查它是否为空.做过这样的事但却无法正常工作.

if [ -s diff.txt ]
then
        touch empty.txt
        rm full.txt
else
        touch full.txt
        rm emtpy.txt
fi
Run Code Online (Sandbox Code Playgroud)

bash if-statement file-handling is-empty

154
推荐指数
8
解决办法
27万
查看次数

如何缩短我的条件陈述

我有一个很长的条件语句,如下所示:

if(test.type == 'itema' || test.type == 'itemb' || test.type == 'itemc' || test.type == 'itemd'){
    // do something.
}
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以将这个表达式/语句重构为更简洁的形式.

有关如何实现这一点的任何想法?

javascript if-statement

153
推荐指数
7
解决办法
2万
查看次数

反转"if"是否会提高性能?

我一直在使用ReSharper一段时间,有时它暗示我反转了if.我想一个例子可以更好地解释我的情况:

public void myfunction(int exampleParam){
    if(exampleParam > 0){
        //Do something with form controls for example.
    }
}
Run Code Online (Sandbox Code Playgroud)

现在ReSharper建议我将其反转if为:

public void myfunction(int exampleParam){
    if(exampleParam <= 0)
        return;
    //Do something with form controls for example
}
Run Code Online (Sandbox Code Playgroud)

这会以某种方式"改善"性能吗?还是仅仅是审美?

c# resharper if-statement

147
推荐指数
0
解决办法
7842
查看次数

如果条件A匹配,则需要匹配条件B以执行动作C.

我的问题是:

if (/* condition A */)
{
    if(/* condition B */)
      {
         /* do action C */
      }
    else
      /* ... */
}
else
{
   /* do action C */
}
Run Code Online (Sandbox Code Playgroud)

是否可以只编写一次C代码而不是两次?

如何简化?

language-agnostic boolean-logic if-statement conditional-statements

147
推荐指数
8
解决办法
1万
查看次数