GVKun编程网logo

java indexof(String str)方法的复杂性(java indexof方法!=-1)

22

对于想了解javaindexof的读者,本文将是一篇不可错过的文章,我们将详细介绍Stringstr方法的复杂性,并且为您提供关于C#中indexOf、lastIndexOf、subString方法的

对于想了解java indexof的读者,本文将是一篇不可错过的文章,我们将详细介绍String str方法的复杂性,并且为您提供关于C# 中indexOf、lastIndexOf、subString方法的理解、C#中String类的几个方法(IndexOf、LastIndexOf、Substring)、delphi – TStringList.IndexOf:indexof中的通配符?、Enum.valueOf(String)方法从何而来?的有价值信息。

本文目录一览:

java indexof(String str)方法的复杂性(java indexof方法!=-1)

java indexof(String str)方法的复杂性(java indexof方法!=-1)

java indexof(Stringstr)方法的复杂性是什么?我的意思是,有像KMP这样的字符串匹配算法可以在线性时间内运行。我正在实现一个需要在非常大的字符串中搜索大子字符串的系统,因此我可以使用java
indexof(String str)方法还是应该实现KMP。

答案1

小编典典

在Java的复杂执行的indexOfO(m*n)其中nm分别搜索字符串和模式的长度。

您可以采取的措施来提高复杂性,例如使用Boyer-
More算法来智能地跳过比较字符串中与模式不匹配的逻辑部分的比较。

C# 中indexOf、lastIndexOf、subString方法的理解

C# 中indexOf、lastIndexOf、subString方法的理解

一、indexOf()

indexOf("\\"):返回"\\"字符在此实例中第一个出现的索引位置,实例的下标是从0开始,如果未找到则返回-1.

indexOf("\\", 7):返回在此实例中从下标7开始的,第一次出现"\\"的位置,如果未找到返回-1.

二、lastIndexOf()

lastIndexOf("\\"):返回"\\"在此实例中最后一个出现的索引位置。即从右向左搜索,第一次出现的"\\"的位置,如果未找到则返回-1.

lastIndexOf("\\", 7):返回在此实例中从下标0开始到下标7结束的这一段子串中,最后一次出现"\\"的位置 。即从右向左搜索,第一次出现的"/"的位置,如果未找到则返回-1.

三、subString()

Substring:截取字符串。Substring(7,2)表示从下标7开始,截取长度为2的字符串,Substring(7)表示从下标7开始,一直截取到字符串末尾。

 

PS:indexOf和lastIndexOf的区别就搜索的方向不一样,indexOf是从左向右,lastIndexOf是从右向左,尽管搜索方向不一样,但是字符下标依然从左向右加1,从0开始。

四、例子:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string FilePath = "D:\\files\\small.txt";
            Console.WriteLine(FilePath);

            int a= FilePath.IndexOf("f");
            Console.WriteLine(a);

            int b = FilePath.IndexOf("s",3);
            Console.WriteLine(b);

            int c = FilePath.LastIndexOf("x");
            Console.WriteLine(c);

            int d = FilePath.LastIndexOf("s",15);
            Console.WriteLine(d);

            string e = FilePath.Substring(7,2);
            Console.WriteLine(e);

            string f = FilePath.Substring(7);
            Console.WriteLine(f);

            int index = FilePath.LastIndexOf(''\\'');
            Console.WriteLine(index);
            string folder = FilePath.Substring(0, index);
            Console.WriteLine(folder);
            string ShapeName = FilePath.Substring(index + 1);
            Console.WriteLine(ShapeName);

            Console.ReadKey();
        }
    }
}

运行结果:

 

C#中String类的几个方法(IndexOf、LastIndexOf、Substring)

C#中String类的几个方法(IndexOf、LastIndexOf、Substring)

下面是小编 jb51.cc 通过网络收集整理的代码片段。

小编小编现在分享给大家,也给大家做个参考。

String.IndexOf

String.LastIndexOf

String.Substring

String.Substring 方法
从此实例检索子字符串。 示例:

总结一下:

以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。

delphi – TStringList.IndexOf:indexof中的通配符?

delphi – TStringList.IndexOf:indexof中的通配符?

我想在字符串列表中检索linenumber(从文件加载).
Indexof似乎完全匹配.有没有办法检索带有通配符版本的Indexof的行?像SL.Indexof(‘?sometext’)这样的东西?

谢谢!

解决方法

如果你想匹配字符串的某些部分,没有任何花哨的通配符,正如你在另一个答案的注释中指出的那样,那么你可以使用这样一个简单的函数:
function FindMatchStr(Strings: TStrings; const SubStr: string): Integer;
begin    
  for Result := 0 to Strings.Count-1 do
    if Containsstr(Strings[Result],SubStr) then
      exit;
  Result := -1;
end;

如果你想要一个不区分大小写的匹配,那么你可以使用这个:

function FindMatchText(Strings: TStrings; const SubStr: string): Integer;
begin    
  for Result := 0 to Strings.Count-1 do
    if ContainsText(Strings[Result],SubStr) then
      exit;
  Result := -1;
end;

Containsstr和ContainsText在StrUtils RTL单元中定义,并遵循Str的标准约定来表示区分大小写的比较,而Text则表示不区分大小写.

Enum.valueOf(String)方法从何而来?

Enum.valueOf(String)方法从何而来?

在Java SE 7中(并且很可能在以前的版本中),Enum类的声明如下:

 public abstract class Enum<E extends Enum<E>> extends Object implements Comparable<E>, Serializable

Enum类具有带有此签名的静态方法:

  T static<T extends Enum<T>> valueOf(Class<T> enumType, String name)

但是没有静态方法:valueOf(String)在Enum类中定义,也不在Enum所属的层次结构中。

问题是valueOf(String)从哪里来的?它是语言的功能,即编译器中内置的功能吗?

答案1

小编典典

该方法由编译器隐式定义。

从文档中:

请注意,对于特定的枚举类型T,可以使用对该枚举上隐式声明的公共静态T
valueOf(String)方法来代替此方法,以从名称映射到相应的枚举常量。可以通过调用该类型的隐式公共静态T []
values()方法来获取枚举类型的所有常量。

根据Java语言规范的第8.9.2节:

另外,如果E是枚举类型的名称,则该类型具有以下隐式声明的静态方法:

/*** Returns an array containing the constants of this enum * type, in the order they''re declared.  This method may be* used to iterate over the constants as follows:**    for(E c : E.values())*        System.out.println(c);** @return an array containing the constants of this enum * type, in the order they''re declared*/public static E[] values();/*** Returns the enum constant of this type with the specified* name.* The string must match exactly an identifier used to declare* an enum constant in this type.  (Extraneous whitespace * characters are not permitted.)* * @return the enum constant with the specified name* @throws IllegalArgumentException if this enum type has no* constant with the specified name*/public static E valueOf(String name);

今天的关于java indexofString str方法的复杂性的分享已经结束,谢谢您的关注,如果想了解更多关于C# 中indexOf、lastIndexOf、subString方法的理解、C#中String类的几个方法(IndexOf、LastIndexOf、Substring)、delphi – TStringList.IndexOf:indexof中的通配符?、Enum.valueOf(String)方法从何而来?的相关知识,请在本站进行查询。

本文标签: