小编Kev*_*vin的帖子

在Java中查找字符串中出现的所有子字符串

我试图在Java中查找字符串中所有出现的子字符串.

例如:搜索"ababsdfasdfhelloasdf"代表"asdf"将返回[8,17],因为有2个"asdf",一个位于8位,一个位于17位.搜索"aaaaaa"中的"aa"将返回[0,1, 1,2,3,4]因为在位置0,1,2,3和4处有"aa".

我试过这个:

public List<Integer> findSubstrings(String inwords, String inword) {
    String copyOfWords = inwords;
    List<Integer> indicesOfWord = new ArrayList<Integer>();
    int currentStartIndex = niwords.indexOf(inword);
    int indexat = 0;
    System.out.println(currentStartIndex);
    while (cthing1 > 0) {
        indicesOfWord.add(currentStartIndex+indexat);
        System.out.println(currentStartIndex);
        System.out.println(indicesOfWord);
        indexat += cthing1;
        copyOfWords = copyOfWords.substring(cthing1);
        System.out.println(copyOfWords);
        cthing1 = copyOfWords.indexOf(inword);
    }
Run Code Online (Sandbox Code Playgroud)

这个问题可以在Python中解决如下:

indices = [m.start() for m in re.finditer(word, a.lower())]
Run Code Online (Sandbox Code Playgroud)

"word"是我正在寻找的单词,"a"是我正在搜索的字符串.

我怎样才能在Java中实现这一目标?

java regex string substring

9
推荐指数
2
解决办法
1万
查看次数

将字符串列表更改为列表

所以,我将一个列表保存为一个字符串文件.特别是,我做了:

f = open('myfile.txt','w')
f.write(str(mylist))
f.close()
Run Code Online (Sandbox Code Playgroud)

但是,稍后当我再次打开此文件时,请使用(string-ified)列表,并希望将其更改回列表,会发生以下情况:

>>> list('[1,2,3]')
['[', '1', ',', '2', ',', '3', ']']
Run Code Online (Sandbox Code Playgroud)

我可以这样做,以便从文件中获得列表[1,2,3]吗?

python python-3.x

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

单击 python 时的 Tkinter 浮雕按钮

因此,在 Tkinter 中,按钮似乎具有浮雕属性。这默认为凸起,当按下按钮时,它会变为凹陷。我使用了relief=FLAT,但是当我点击这个按钮时它仍然是凹陷的。有没有办法让它在点击时有不同的浮雕?它会涉及更改边框颜色吗?

Button(text=day, width=2,
       relief=FLAT, activebackground = self.color_clicked,
       background = self.today_color)
Run Code Online (Sandbox Code Playgroud)

这看起来很平坦,但是当我点击它时,它会变成下沉的(很明显,当我松开时它会变回平面)。

python tkinter button

2
推荐指数
2
解决办法
1万
查看次数

Tkinter:为按钮和标签制作"类"

所以,我在tkinter框架中有很多不同的按钮和标签,我都希望它们具有相似的属性.让我们说我希望他们所有人都有一个红色的前景色,并有一个透明的背景(我甚至可以这样做吗?这个透明背景只适用于按钮.)

我可以有一个class按钮(我认为这是在ttk,但它会更好,如果它不是)类似于CSS会使我的所有按钮和标签都有红色文字?

python tkinter python-3.x

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

〜1和~0在python 3中给出了奇怪的结果

&,|,^和〜都是python中的按位运算符.&,^和| 对我来说都很好 - 当我拿1 | 0时,我得到1.但是〜给了我奇怪的结果.~1给我-2,而0给我-1.这是因为我使用的是整数还是其他什么?我正在运行python 3.

我希望从〜0得到1,从〜1得到0(整数).这可能吗?

python bit-manipulation bitwise-operators python-3.x

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