【问题标题】:location and repetition of characters within a string字符串中字符的位置和重复
【发布时间】:2016-06-09 05:23:26
【问题描述】:

您好,这里有一点编码背景的生物学家。我的目标是能够输入一串字符和代码,以便能够告诉我它们出现了多少次以及在字符串中的哪个位置。 所以我要输入一个字符串,我想要字符串中 sq 和 tq​​ 的位置和数量。位置是第一个字符,例如 njnsqjjfl sq 将位于位置 4。

这是我到目前为止的想法(可能非常错误)

string S = "...";
int counter =0;
for(int i=0; i<s.length; i++){
if(s.charAt (i) == 'sq')}
counter++;})

string S = "...";
int counter =0;
for(int i=0; i<s.length; i++){
if(s.charAt (i) == 'tq')}
counter++;})

任何意见都会有所帮助,谢谢

【问题讨论】:

标签: java string counter


【解决方案1】:

因此,您可以在代码中多次出现“sq”和“tq”,因此您可以有 2 个数组列表来分别保存这两个(或一个来将它们保存在一起)。

ArrayList<Integer>sqLocation = new ArrayList<>();
ArrayList<Integer>tqLocation = new ArrayList<>();
for(int i =0;i<s.length()-1;i++){
if(s.charAt(i)=='s' && s.charAt(i+1)=='q'){
  sqLocation.add(i);
  }
else if(s.charAt(i)=='t' && s.charAt(i+1)=='q'){
  tqLocation.add(i);
  }
}
System.out.println("No. of times sq occurs = "+sqLocation.size());
System.out.println("Locations ="+sqLocation);
System.out.println("No. of times tq occurs = "+tqLocation.size());
System.out.println("Locations ="+tqLocation);

【讨论】:

    【解决方案2】:

    这可以使用正则表达式来实现。您的用例是计算这些事件的出现次数和位置。 match 方法返回一个整数列表,它是位置,计数是列表的大小

    示例代码

    public class RegexTest {
        public static List<Integer> match(String text, String regex) {
            List<Integer> matchedPos = new ArrayList<>();
            Matcher m = Pattern.compile("(?=(" + regex + "))").matcher(text);
            while(m.find()) {
                matchedPos.add(m.start());
            }
    
            return matchedPos;
        }
    
        public static void main(String[] args) {
            System.out.println(match("sadfsagsqltrtwrttqsqsqsqsqsqs", "sq"));
            System.out.println(match("sadfsagsqltrtwrttqksdfngjfngjntqtqtqtqtqtq", "tq"));
        }
    }
    

    【讨论】:

      【解决方案3】:

      你想要的是HashMap &lt;String, List &lt;Integer&gt;&gt;

      这将保留您正在寻找的字符串,例如sq 或 tq,以及他们所在位置的列表。

      您想使用String.indexOf 循环查看https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int)

      伪代码存在

      String contents = "sadfsagsqltrtwrttqksdfngjfngjntqtqtqtqtqtq";
      
      map.add (lookFor, new ArrayList ());  
      
      int index = 0;
      while ((index = contents.indexOf (lookFor, index)) != -1) {
      
        list = map.get (lookFor);
        list.add (index);
      }
      

      【讨论】:

        【解决方案4】:

        您不应该使用charAt,而是使用substring 来获取String 的一部分。

        int count(String s, String target) {
            int counter = 0;
            int tlen = target.length();
            for (int i = tlen; i < s.length(); i++) {
                if (s.substring(i - tlen, i).equals(target)) {
                    counter++;
                }
            }
            return counter;
        }
        
        // in some method
        count("...", "sq");
        count("...", "tq");
        

        【讨论】:

        • 给出计数但不给出and at what location in the string
        猜你喜欢
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-14
        • 2012-06-22
        • 2015-06-26
        • 2014-02-04
        相关资源
        最近更新 更多