小编Sou*_*ape的帖子

"在char []中查找子字符串"获得意外结果

免责声明:这是一个功课问题.我正在尝试编写一个contains(java.lang.String subString)方法,该方法返回一个int表示主字符串中比较字符串索引的值,用于定制的String类.

一些规则:

  • 没有收集课程
  • java String类中只允许使用charAt()和toCharArray()(但允许来自其他类的方法)
  • 假设length()返回主字符串的长度(这正是它的作用)

我的代码:

public int contains(java.lang.String subString) {
    this.subString = subString;
    char[] arrSubStr = this.subString.toCharArray();
    //Create initial fail
    int index = -1;
    //Make sure comparison subString is the same length or shorter than the primary string
    if(arrSubStr.length > length()) {
        return index;
    }
    //Steps to perform if initial conditions are met
    else {
        //Compare first character of subString to each character in primary string
        for(int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

java arrays methods char

6
推荐指数
1
解决办法
79
查看次数

Bash 脚本适用于终端模拟器,但不适用于 i3 键绑定

我有一个锁屏脚本(通过 i3lock),它可以在终端窗口中正确运行,但在 i3 配置中用作键绑定时却不能。

脚本非常简单:

# Take screenshot
scrot /tmp/screenshot.png

# Pixelate and add central logo
magick /tmp/screenshot.png -scale 10% -scale 1000% -gravity center /home/user/bin/archlogo.png -composite /tmp/lock.png

# Lock using pixelated image & logo
i3lock -e -f -c 000000 -i /tmp/lock.png
Run Code Online (Sandbox Code Playgroud)

脚本被保存~/bin/lock并可执行。该目录也已在我的~/.bash_profile.

没有错误信息,只有终端光标的短暂闪烁。

中的相关行~/.config/i3/config

# lock screen
bindsym $mod+l exec lock
Run Code Online (Sandbox Code Playgroud)

(删除了 $mod+l 的默认键绑定并重新启动了 i3)。

linux bash archlinux i3

5
推荐指数
1
解决办法
1518
查看次数

在两个文本文件之间寻找长度> 4的匹配字符串

我正在尝试读取两个文本文件,然后搜索每个文本文件中最常长度为5的字符串.

我写的代码:

db = open("list_of_2","r").read()
lp = open("lastpass","r").read()

word = ''
length = 0

for dbchar in db:
    for lpchar in lp:
        if dbchar == lpchar:
            word += str(dbchar)
            length += 1
        else:
            length = 0
            word = ''
        if length > 4:
            print(word)
Run Code Online (Sandbox Code Playgroud)

代码当前一遍又一遍地打印像'-----'和'55555'这样的字符串,似乎没有打破循环(这些特殊的字符串只出现lp一次).我也不相信这是找到字符串,一样的角色重复.

如何将代码更改为:

  1. 只让它贯穿并打印每次出现一次,然后
  2. 不只是找到重复相同字符的字符串?

编辑:这是一些模拟文本文件.在这些中,字符串'ghtyty'在file1中出现三次,在file2中出现4次.代码应该打印'ghtyty'一次到控制台.

file1 file2

python

0
推荐指数
1
解决办法
48
查看次数

标签 统计

archlinux ×1

arrays ×1

bash ×1

char ×1

i3 ×1

java ×1

linux ×1

methods ×1

python ×1