public boolean catDog(String str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
String sub = str.substring(i, i+1);
if (sub.equals("cat") && sub.equals("dog"))
count++;
}
return count == 0;
}
Run Code Online (Sandbox Code Playgroud)
有我的catDog代码,已经工作了一段时间而且无法找出问题所在.非常感谢帮助!*/
编辑 - 如果字符串"cat"和"dog"在给定字符串中出现的次数相同,我想返回true.
我有一列值,如:
[{"run_status":1,"daily_budget":"2000","campaign_id":"60952315"}].
Run Code Online (Sandbox Code Playgroud)
删除第一个和最后一个括号的查询是什么,所以我留下了
{"run_status":1,"daily_budget":"2000","campaign_id":"60952315"}
Run Code Online (Sandbox Code Playgroud)
如果您的答案取决于SQL版本,我使用的是Hive.
我有一个有两个字符的NSString.
NSSring看起来像这样
FW
我使用此代码捕获第一个字符
NSString *firstStateString = [totInstStateString substringToIndex:1];
Run Code Online (Sandbox Code Playgroud)
这段代码返回F,我想知道如何使用substringToIndex将第二个字符返回到自己的字符串.
任何帮助,将不胜感激.
我在X列的每个值的末尾都有这些特殊字符"||〜||".我需要删除这些特殊字符.
现在我正在使用这种语法,但似乎并没有完成所有行的任务.
set [Customer Num] = substring(ltrim(rtrim([Customer Num])),3,len([Customer Num]))
Run Code Online (Sandbox Code Playgroud) 通常,我使用String.sustring(int, int)方法来获取文本的某些部分.如果文字太大,那么我无法使用Integer类型变量访问某个位置,有什么方法可以解决这个问题?
例如:
我想用 String.substring(Long, Long)
我想从字符串中提取子字符串,这符合某个正则表达式.正则表达式是:
(\[\s*(\d)+ byte(s)?\s*\](\s*|\d|[A-F]|[a-f])+)
这实际上意味着所有这些字符串都被接受:
[4 bytes] 66 74 79 70 33 67 70 35
[ 4 bytes ] 66 74 79 70 33 67 70 35
[1 byte] 66 74 79 70 33 67 70 35
Run Code Online (Sandbox Code Playgroud)
我想从这个字符串中只提取字节数(只是数字).我想过这样做re.search,但我不确定这是否会奏效.这样做最干净,最高效的方法是什么?
我在SUBSTRING中有CHARINDEX函数,它必须识别分隔符(|)并将值作为参数返回给子字符串
当分隔符存在时,它工作正常
select SUBSTRING('SH1684|32I5', 1, CHARINDEX('|', 'SH1684|32I5') -1) AS ID
Run Code Online (Sandbox Code Playgroud)
其中ID是SH1684
当它不存在时
这会导致错误
消息536,级别16,状态1,行1传递给子字符串函数的长度参数无效.
但我的目的是获得ID作为SH168432I5的价值.
我怎样才能避免这个问题.
我有一个String变量,每次运行时都可以有不同的长度.有了它,我会检查它的开头,例如:
public void defineLocation(){
if (newLocation.substring(0,2).equals("DO") || newLocation.substring(0,2).equals("30") || newLocation.substring(0,2).equals("21")) {
locationInDc = "DOOR";
} else if (newLocation.substring(0,2).equals("VT") || newLocation.substring(0,3).equals("MUF")) {
locationInDc = "BLOUBLOU";
} else if (newLocation.substring(0,3).equals("MAH")) {
locationInDc = "BLOBLO";
} else if (newLocation.substring(0,7).equals("Zone 72") || newLocation.substring(0,7).equals("Zone 70")){
locationInDc = "BLOFBLOF";
}
Run Code Online (Sandbox Code Playgroud)
我知道这不是最有效的方式,它必然会破坏,如果我的变量不在前3个检查中的任何一个但仍然具有比7更少的字符,那么它将抛出错误.
有没有更"正确"的方法来做到这一点?我应该首先检查字符串包含多少个字符,然后将其指向正确的检查/"ifs"?谢谢.
我是编程和处理Java String分配的新手.问题是:
这是我做的:
String phrase = new String ("This is a String test.");
String middle3 = new String ("tri"); //I want to print the middle 3 characters in "phrase" which I think is "tri".
middle3 = phrase.substring (9, 11); // this only prints out 1 letter instead of 3
System.out.println ("middle3: " + middle3);
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
Original phrase: This is a String test.
Length of the phrase: 18 characters
middle3: S
Run Code Online (Sandbox Code Playgroud)
我还认为字符串"短语"中有18个字符,但如果没有,请告诉我.提前感谢您的帮助!
我的初始字符串是
Content-Disposition: attachment; filename="0001.zam"
Run Code Online (Sandbox Code Playgroud)
我想选择两个“”字符之间的所有字符(在这种情况下为“ 0001.zam”。我知道我需要使用类似于以下内容的SUBSTRING和CHARINDEX函数:
SELECT SUBSTRING(@Attachment, CHARINDEX('"', @Attachment),...)
Run Code Online (Sandbox Code Playgroud)
我不知道要作为第二个SUBSTRING参数传递什么。请注意,两个“字符”之间的字符串和第二个“字符”之后的字符串是可变的。整个字符串看起来像。像这样:
Content-Disposition: attachment; filename="0001556.txt"; size=187;
Run Code Online (Sandbox Code Playgroud)
最重要的是获得两个“”字符之间的所有内容。
substring ×10
java ×4
string ×4
sql-server ×3
charindex ×2
case ×1
hive ×1
if-statement ×1
ios ×1
long-integer ×1
nsstring ×1
python ×1
regex ×1
sql ×1
t-sql ×1