java 如果字符串中的所有字母都相同,则尝试返回 true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40569540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to return true if all the letters in a string are the same
提问by Harry You
What I have so far:
到目前为止我所拥有的:
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
int charb4 = i--;
if ( str.charAt(i) != str.charAt(charb4))
{
return false;
}
if ( i == str.length())
{
return true;
}
}
}
Please excuse any inefficiencies if any; still relatively new to coding in general. Am I lacking some knowledge in terms of using operators and .charAt() together? Is it illogical? Or is my error elsewhere?
如果有任何效率低下,请原谅;总的来说,对于编码来说还是相对较新的。我在使用运算符和 .charAt() 方面缺乏一些知识吗?这不合逻辑吗?还是我的错误在其他地方?
回答by developer
You can follow the below steps:
您可以按照以下步骤操作:
(1) Get the first character (i.e., 0th index)
(1) 获取第一个字符(即第0个索引)
(2) Check the first character is the same with subsequent characters, if not return false
(and comes out from method)
(2)检查第一个字符是否与后面的字符相同,如果不相同则返回false
(并从方法中出来)
(3) If all chars match i.e., processing goes till the end of the method and returns true
(3) 如果所有字符都匹配ie,则处理直到方法结束并返回 true
public boolean allSameLetter(String str) {
char c1 = str.charAt(0);
for(int i=1;i<str.length;i++) {
char temp = str.charAt(i);
if(c1 != temp) {
//if chars does NOT match,
//just return false from here itself,
//there is no need to verify other chars
return false;
}
}
//As it did NOT return from above if (inside for)
//it means, all chars matched, so return true
return true;
}
回答by Gayan Weerakutti
Using regex:
使用正则表达式:
return str.matches("^(.)\1*$");
Using streams:
使用流:
str.chars().allMatch(c -> c == str.charAt(0));
Other:
其他:
return str.replace(String.valueOf(str.charAt(0), "").length() == 0;
回答by Matthew Wright
As Andrew said, you are decreasing i
within your for loop. You can fix this by changing it to int charb4 = i - 1;
. As for making your code more efficient you could condense it down to this.
正如安德鲁所说,您i
在 for 循环中正在减少。您可以通过将其更改为 来解决此问题int charb4 = i - 1;
。至于使您的代码更高效,您可以将其浓缩为这一点。
public boolean allSameLetter(String str) {
for(char c : str.toCharArray())
if(c != str.charAt(0)) return false;
return true;
}
回答by Gabrielus
Comment if you don't understand a part of it :)
如果您不明白其中的一部分,请发表评论:)
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length() -1; i++)
{
if ( str.charAt(i) != str.charAt(i+1))
{
return false;
}
}
return true
}
-1 is there since I am checking the current value in the array, then the next value in the array, thus I need to stop a place earlier.
-1 是存在的,因为我正在检查数组中的当前值,然后是数组中的下一个值,因此我需要提前停止一个地方。
If the loop if statement is never entered, it will make it far enough into the code to return true
如果从不输入循环 if 语句,它将使其进入代码足够远以返回 true
回答by Carter Cobb
You have to create a for loop that searches through the length of the String - 1. this way the program will not crash because of a 3 letter word with the program trying to get the 4th letter. This is what works for me:
您必须创建一个 for 循环来搜索 String - 1 的长度。这样程序就不会因为 3 个字母的单词而导致程序尝试获取第 4 个字母而崩溃。这对我有用:
public boolean allSameLetter(String str)
{
for(int i = 0; i< str.length()-1; i++){
if (str.charAt(i) != str.charAt(i+1)){
return false;
}
}
return true;
}
回答by Abhik Chakraborty
if((new HashSet<Character>(Arrays.asList(s.toCharArray()))).size()==1)
return true;
return false;
if((new HashSet<Character>(Arrays.asList(s.toCharArray()))).size()==1)
return true;
return false;
This should be enough
这应该够了
回答by Hendrik T
The bug is caused by
该错误是由
int charb4 = i--;
this line is equal to
这条线等于
int charb4 = i-1;
i=i-1;
Because of this, your loop will never stop.
The easiest way to fix this
因此,您的循环将永远不会停止。
解决这个问题的最简单方法
public boolean allSameLetter(String str)
{
for (int i = 1; i < str.length(); i++)
{
if ( str.charAt(i) != str.charAt(i-1))
{
return false;
}
}
}