如何简单地获取java中字符串前缀上所有出现的字符?

发布于 2024-11-10 05:45:47 字数 624 浏览 0 评论 0原文

我有一个像这样的字符串:'0010'

如何获取该示例字符串的前两个零。这里的规则是,我有一个保存字符的变量。然后我需要查看字符串,如果字符串的第一个字符与变量值相同。我需要保留它,然后如果第二个字符串再次匹配,则将其连接起来,依此类推。如果字符串的第一个字符与变量值不匹配,则它将不会存储也不会再次查找前一个字符。

虽然我已经有了解决方案,但我使用了大约 10 行代码来做到这一点。

这是我的代码:

    String start = "0001";
    String concatVal = "";
    char prefix = '0';

    for(int i = 0; i < start.length(); i++){
        if(start.charAt(i) == prefix){
            concatVal += prefix;
        } else {
            break;
        }
    }
    System.out.println(concatVal);

            //Output
            000

如果有更简单的方法来实现此目的,请告诉我。谢谢

I have a string like this: '0010'

How can I get the first two zero on that sample string. The rules here is that, I have a variable which hold a character. Then I need to look on the string, if the first character of the string is same with the variable value. I need to keep it and then if the second string matches again, concatenate it and so on. If the first character of the string is not matched with the variable value then it will not store and not look again on the preceding character.

Though I have already solution but I used about 10 lines of codes to do this.

Here is my code:

    String start = "0001";
    String concatVal = "";
    char prefix = '0';

    for(int i = 0; i < start.length(); i++){
        if(start.charAt(i) == prefix){
            concatVal += prefix;
        } else {
            break;
        }
    }
    System.out.println(concatVal);

            //Output
            000

If there is a more simple way to achieve this, please let me know. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梨涡 2024-11-17 05:45:47

您可以在不逐字符构建结果字符串的情况下完成此操作。相反,查找可能匹配的长度,然后从原始字符串中剪切。

int length = 0;
while (length < start.length() && start.charAt(length) == prefix) {
    length++;
}
String concatVal = start.substring(0, length);

You may do it without building the result string char-by-char. Instead look for the length of the possible match and then cut from the original string.

int length = 0;
while (length < start.length() && start.charAt(length) == prefix) {
    length++;
}
String concatVal = start.substring(0, length);
友谊不毕业 2024-11-17 05:45:47

看起来不错,一个可能的改变是

for(char ch : start.toCharArray())
{
if(ch == prefix)
{
   concatVal += prefix; 
} else {    
   break; 
 }
}

Looks pretty good, one possible change would be

for(char ch : start.toCharArray())
{
if(ch == prefix)
{
   concatVal += prefix; 
} else {    
   break; 
 }
}
空城仅有旧梦在 2024-11-17 05:45:47

你的代码已经很简单了。英文描述并不短,所以不用担心。通过一点点重写,您会得到:

public static String prefixOf(String s, char prefix) {
  for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) != prefix) {
      return s.substring(0, i);
    }
  }
  return s;
}

这个定义只有四行实际长度:

  1. 任何算法总是需要方法定义。不算数。
  2. 计数。
  3. 计数。
  4. 计数。
  5. 只有一个右大括号,可以省略。
  6. 只有一个右大括号,可以省略。
  7. 计数。
  8. 每个方法都必须以右大括号结束。不算数。

与您的描述相比,这确实很短。

Your code is already very simple. The English description isn't much shorter, so don't worry. With a little bit of rewriting you get:

public static String prefixOf(String s, char prefix) {
  for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) != prefix) {
      return s.substring(0, i);
    }
  }
  return s;
}

This definition is only four real lines long:

  1. The method definition is always needed for any algorithm. Doesn't count.
  2. Counts.
  3. Counts.
  4. Counts.
  5. Only a closing brace, which could be omitted.
  6. Only a closing brace, which could be omitted.
  7. Counts.
  8. Every method has to end with a closing brace. Doesn't count.

Compared to your description, this is really short.

情归归情 2024-11-17 05:45:47

您只需从原始字符串中提取结果即可。

String text = "0001";
char prefix = '0';
String result = text;

for(int i = 0; i < text.length(); i++){
    if(text.charAt(i) != prefix) {
        result = text.substring(0, i);
        break;
    }
}
System.out.println(result);

You could just extract the result from the original string.

String text = "0001";
char prefix = '0';
String result = text;

for(int i = 0; i < text.length(); i++){
    if(text.charAt(i) != prefix) {
        result = text.substring(0, i);
        break;
    }
}
System.out.println(result);
一绘本一梦想 2024-11-17 05:45:47
    String start="0001";
    String concat="";
    String prefix="0";
    for(int i=0;i<start.length();i++){
        if(!prefix.contains(String.valueOf(start.charAt(i)))){
            concat=start.substring(0, i);
            break;
        }
    }
    System.out.println(concat);
    String start="0001";
    String concat="";
    String prefix="0";
    for(int i=0;i<start.length();i++){
        if(!prefix.contains(String.valueOf(start.charAt(i)))){
            concat=start.substring(0, i);
            break;
        }
    }
    System.out.println(concat);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文