标签: substring

如何使用Perl在字符串中找到子字符串?

我有一个字符串,我希望从中提取一个单词,但附加一个数字,每行可能不同:

This is string1 this is string
This is string11 
This is string6 and it is in this line
Run Code Online (Sandbox Code Playgroud)

我想解析这个文件并获取"stringXXX"的值,从0到100开始

# suppose ABC.txt contains the above lines
FH1 = open "Abc.txt"; 
@abcFile = <FH1>;

foreach $line(@abcFile) {
    if ($pattern =~ s/string.(d{0}d{100});
        print $pattern;
Run Code Online (Sandbox Code Playgroud)

以上打印整行,我希望只获得stringXXX

regex perl substring

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

通过子字符串快速过滤字符串集合?

您是否知道快速过滤字符串列表以获取包含指定字符串的子集的方法?显而易见的实现是遍历列表,检查每个字符串是否包含搜索字符串.有没有办法索引字符串列表,以便可以更快地完成搜索?

string algorithm search substring

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

在Java中修剪字符串,同时保留完整的单词

我需要修剪java中的String,以便:

快速的棕色狐狸跳过laz狗.

快棕...

在上面的例子中,我修剪为12个字符.如果我只使用子字符串,我会得到:

快速...

我已经有了使用子字符串执行此操作的方法,但我想知道什么是最快(最有效)的方法,因为页面可能有许多修剪操作.

我能想到的唯一方法是将字符串分割成空格并将其重新组合,直到其长度超过给定长度.还有其他方法吗?也许是一种更有效的方法,我可以使用相同的方法来执行"软"修剪,其中我保留最后一个单词(如上例所示)和硬修剪,这几乎是一个子字符串.

谢谢,

java string substring trim

9
推荐指数
3
解决办法
7345
查看次数

substring并返回特定字符后的值

"Testing.BSMain,Text:Start Page"

我想将上面的值子字符串并仅返回vb.net中":"之后的值.我怎样才能做到这一点?

vb.net substring

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

Java中的子字符串 - 长度最大为一个值

我正在尝试创建一个子字符串,这个子字符串最多可以包含6个字母的字母,但是当我找到一个少于6个字母的姓氏时,我在这里似乎会抛出错误,我一直在寻找解决方案的时间没有成功:/

id = firstName.substring (0,1).toLowerCase() + secondName.substring (0,6).toLowerCase();
System.out.print ("Here is your ID number: " + id);
Run Code Online (Sandbox Code Playgroud)

就是这样.substring(0,6).我需要它最多 6个字母而不是6个字母.

错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
    at java.lang.String.substring(Unknown Source)
    at Test.main(Test.java:27)
Run Code Online (Sandbox Code Playgroud)

java string substring

9
推荐指数
3
解决办法
5476
查看次数

在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万
查看次数

Kotlin:如何在字符串中的"@"之后获取字符?

我有一个电子邮件的字符串.无论字符串/电子邮件是什么,我希望能够获得电子邮件的域名部分.基本上我想要在字符串的@部分之后抓住字符.例如,对于testing@kotlin.com,我在kotlin.com部分之后.

val emailString ="hello@world.com"

string android substring string-interpolation kotlin

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

如何检查对象是否至少包含一个键,其值包含 JavaScript 中的子字符串?

我想编写一个函数来检查一个对象是否至少有一个包含子字符串的值。像这样(伪代码):

const userMatchesText = (text, user) => user.includes(text);
Run Code Online (Sandbox Code Playgroud)

我的对象的完整结构(

因此,对于像以下这样的用户:

const user = {
    id: '123abc',
    info: {
        age: 12,
        bio: 'This is my bio' 
    },
    social: {
        chatName: 'Chris',
        friends: ['friend1', 'other friend'],
        blocks: ['Creep']
    }
    //Etc. The objects I'm working with contain nested objects and arrays, etc.
}
Run Code Online (Sandbox Code Playgroud)

, userMatches('bi', user)应该返回,true因为子字符串 'bi' 在 bio: 'this is my bio' 中找到。userMatches('324d, user) 同样应该返回falseusermatches('blocks', user)但是,应该返回,false因为子字符串只能在其中一个键中找到,而不是在其中一个值中。

我正在处理的对象如下所示(Mongoose Schema):

{
    account  : { …
Run Code Online (Sandbox Code Playgroud)

javascript string filtering substring object

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

检查熊猫数据框中是否有多个子字符串

我有一个 Pandas 数据框,我想检查某个列的子字符串。目前我有 30 行这样的代码:

df['NAME'].str.upper().str.contains('LIMITED')) |
(df['NAME'].str.upper().str.contains('INC')) |
(df['NAME'].str.upper().str.contains('CORP')) 
Run Code Online (Sandbox Code Playgroud)

它们都与一个or条件相关联,如果其中任何一个为真,则名称是公司的名称而不是个人的名称。

但对我来说,这似乎不是很优雅。有没有办法检查熊猫字符串列中的“此列中的字符串是否包含以下列表中的任何子字符串” ['LIMITED', 'INC', 'CORP']

我找到了 pandas.DataFrame.isin 函数,但这仅适用于整个字符串,不适用于我的子字符串。

python select substring pandas

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

寻找子字符串替代javascript

基本上我的问题是我在变量版本上使用 substring 方法来获取结果,然后使用 ng-href 在 URL 中使用:

substring(0, 3)
version 9.1.0 = 9.1 (good)
version 9.2.0 = 9.2 (good)
version 9.3.0 = 9.3 (good)
..
version 9.10.0 = 9.1 (breaks here)
version 10.1.0 = 10. (breaks here)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,最终子字符串方法停止工作,我该如何解决这个问题?

javascript methods substring angularjs-ng-href

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