質問
のindexOf(String)メソッド大文字と小文字が区別?その場合、大文字と小文字を区別しません版することはできるのでしょうか?
解決
indexOf()
方法は、すべて大文字と小文字が区別されます。あなたは事前に大文字/小文字にあなたの文字列を変換することによって、それら(おおよそ、壊れた方法ではなく、例たくさんのために働いて)大文字と小文字を区別しない作ることができます:
s1 = s1.toLowerCase(Locale.US);
s2 = s2.toLowerCase(Locale.US);
s1.indexOf(s2);
他のヒント
敏感なのindexOf(String)メソッドの場合ですか?
はい、それは大文字と小文字が区別されます:
@Test
public void indexOfIsCaseSensitive() {
assertTrue("Hello World!".indexOf("Hello") != -1);
assertTrue("Hello World!".indexOf("hello") == -1);
}
もしそうなら、それの大文字小文字を区別しないバージョンがありますか?
いいえ、ありません。あなたはのindexOfを呼び出す前に、小文字に両方の文字列を変換することができます:
@Test
public void caseInsensitiveIndexOf() {
assertTrue("Hello World!".toLowerCase().indexOf("Hello".toLowerCase()) != -1);
assertTrue("Hello World!".toLowerCase().indexOf("hello".toLowerCase()) != -1);
}
はApache CommonsのラングライブラリーののStringUtilsクラスのケースメソッドを無視する
がありますindexOfIgnoreCase(たCharSequenceのSTR、searchStrたCharSequence)
はい、indexOf
は大文字と小文字が区別されます。
私が発見した場合のinsensivityを行うための最善の方法はあります:
String original;
int idx = original.toLowerCase().indexOf(someStr.toLowerCase());
これは大文字小文字を区別しないindexOf()
を行います。
ここので、ここで言及した他の実装のほとんどよりもはるかに高速である必要があり、任意のヒープメモリを割り当てません私のソリューションです。
public static int indexOfIgnoreCase(final String haystack,
final String needle) {
if (needle.isEmpty() || haystack.isEmpty()) {
// Fallback to legacy behavior.
return haystack.indexOf(needle);
}
for (int i = 0; i < haystack.length(); ++i) {
// Early out, if possible.
if (i + needle.length() > haystack.length()) {
return -1;
}
// Attempt to match substring starting at position i of haystack.
int j = 0;
int ii = i;
while (ii < haystack.length() && j < needle.length()) {
char c = Character.toLowerCase(haystack.charAt(ii));
char c2 = Character.toLowerCase(needle.charAt(j));
if (c != c2) {
break;
}
j++;
ii++;
}
// Walked all the way to the end of the needle, return the start
// position that this was found.
if (j == needle.length()) {
return i;
}
}
return -1;
}
そして、ここでは、正しい動作を検証するユニットテストがあります。
@Test
public void testIndexOfIgnoreCase() {
assertThat(StringUtils.indexOfIgnoreCase("A", "A"), is(0));
assertThat(StringUtils.indexOfIgnoreCase("a", "A"), is(0));
assertThat(StringUtils.indexOfIgnoreCase("A", "a"), is(0));
assertThat(StringUtils.indexOfIgnoreCase("a", "a"), is(0));
assertThat(StringUtils.indexOfIgnoreCase("a", "ba"), is(-1));
assertThat(StringUtils.indexOfIgnoreCase("ba", "a"), is(1));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", " Royal Blue"), is(-1));
assertThat(StringUtils.indexOfIgnoreCase(" Royal Blue", "Royal Blue"), is(1));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "royal"), is(0));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "oyal"), is(1));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "al"), is(3));
assertThat(StringUtils.indexOfIgnoreCase("", "royal"), is(-1));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", ""), is(0));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "BLUE"), is(6));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "BIGLONGSTRING"), is(-1));
assertThat(StringUtils.indexOfIgnoreCase("Royal Blue", "Royal Blue LONGSTRING"), is(-1));
}
はい、それは大文字と小文字が区別されます。あなたが検索する前に大文字にあなたの文字列と文字列パラメータの両方を変換することによって、大文字と小文字を区別しないindexOf
を行うことができます。
String str = "Hello world";
String search = "hello";
str.toUpperCase().indexOf(search.toUpperCase());
toUpperCaseのは、いくつかの状況では動作しない場合があることに注意してください。このインスタンスのます:
String str = "Feldbergstraße 23, Mainz";
String find = "mainz";
int idxU = str.toUpperCase().indexOf (find.toUpperCase ());
int idxL = str.toLowerCase().indexOf (find.toLowerCase ());
idxUは間違っている、20になります! IDXLは正しいです19になります。何が問題を引き起こしていることは股関節のtoUpperCase()は、2つの文字に「SS」を「ß」文字を変換し、これは、インデックスをオフにスローされます。
このため、常にtoLowerCaseメソッド()に固執
あなたは一度返されるインデックス値で何をしているのか?
あなたは、文字列を操作するためにそれを使用している場合は、、そして、あなたの代わりに正規表現を使用することができませんでした?
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringIndexOfRegexpTest {
@Test
public void testNastyIndexOfBasedReplace() {
final String source = "Hello World";
final int index = source.toLowerCase().indexOf("hello".toLowerCase());
final String target = "Hi".concat(source.substring(index
+ "hello".length(), source.length()));
assertEquals("Hi World", target);
}
@Test
public void testSimpleRegexpBasedReplace() {
final String source = "Hello World";
final String target = source.replaceFirst("(?i)hello", "Hi");
assertEquals("Hi World", target);
}
}
私は、ソースを見てきました。それは大文字と小文字が区別されますので、それは文字を比較します。
@Test
public void testIndexofCaseSensitive() {
TestCase.assertEquals(-1, "abcDef".indexOf("d") );
}
はい、私はそれはかなり確信しています。標準ライブラリを使用するだろうと周りの作業の一つの方法:
int index = str.toUpperCase().indexOf("FOO");
同じ問題を抱えていました。 私は正規表現とApache StringUtils.indexOfIgnoreCase-方法を試してみましたが、両方はかなり遅かったです... だから私は...短い方法を自分で書いてます:
public static int indexOfIgnoreCase(final String chkstr, final String searchStr, int i) {
if (chkstr != null && searchStr != null && i > -1) {
int serchStrLength = searchStr.length();
char[] searchCharLc = new char[serchStrLength];
char[] searchCharUc = new char[serchStrLength];
searchStr.toUpperCase().getChars(0, serchStrLength, searchCharUc, 0);
searchStr.toLowerCase().getChars(0, serchStrLength, searchCharLc, 0);
int j = 0;
for (int checkStrLength = chkstr.length(); i < checkStrLength; i++) {
char charAt = chkstr.charAt(i);
if (charAt == searchCharLc[j] || charAt == searchCharUc[j]) {
if (++j == serchStrLength) {
return i - j + 1;
}
} else { // faster than: else if (j != 0) {
i = i - j;
j = 0;
}
}
}
return -1;
}
私のテストによると、そのはるかに速く...(あなたsearchStringのはかなり短いです、少なくとも場合)。 あなたは改善やバグのための提案があれば(私はアプリケーションでこのコードを使用するので; - )...私が知っているようにいいだろう。
最初の質問で答えました。あり、 String.indexOf()
方法はすべて大文字と小文字が区別される。
が必要な場合は、ロケール感度 indexOf()
ご利用可能に Collator.によっては強度の値を設定しき大文字と小文字を区別しません比べ、治療もアクセントの文字と同じなかったと考えられるが、アクセントもの。この方法について、以下に例を示しないこ
private int indexOf(String original, String search) {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
for (int i = 0; i <= original.length() - search.length(); i++) {
if (collator.equals(search, original.substring(i, i + search.length()))) {
return i;
}
}
return -1;
}
だけで、3つのソリューション
- 使用toLowerCase()やtoUpperCase
- 使用StringUtils apache
- 正規表現を使用
などから持ち直している"という思ったので、最速?私の推測の平均になります。
しかし、それは1を書くことは難しいことではありません。
public class CaseInsensitiveIndexOfTest extends TestCase {
public void testOne() throws Exception {
assertEquals(2, caseInsensitiveIndexOf("ABC", "xxabcdef"));
}
public static int caseInsensitiveIndexOf(String substring, String string) {
return string.toLowerCase().indexOf(substring.toLowerCase());
}
}
小文字に両方の文字列を変換し、通常は大したことではなく、文字列の一部が長い場合には遅くなります。あなたがループ内でこれを行う場合と、それは本当に悪いだろう。このような理由から、私はindexOfIgnoreCase
をお勧めします。
static string Search(string factMessage, string b)
{
int index = factMessage.IndexOf(b, StringComparison.CurrentCultureIgnoreCase);
string line = null;
int i = index;
if (i == -1)
{ return "not matched"; }
else
{
while (factMessage[i] != ' ')
{
line = line + factMessage[i];
i++;
}
return line;
}
}
ここで密接にApacheののStringUtilsバージョンに似たバージョンがあります:
public int indexOfIgnoreCase(String str, String searchStr) {
return indexOfIgnoreCase(str, searchStr, 0);
}
public int indexOfIgnoreCase(String str, String searchStr, int fromIndex) {
// https://stackoverflow.com/questions/14018478/string-contains-ignore-case/14018511
if(str == null || searchStr == null) return -1;
if (searchStr.length() == 0) return fromIndex; // empty string found; use same behavior as Apache StringUtils
final int endLimit = str.length() - searchStr.length() + 1;
for (int i = fromIndex; i < endLimit; i++) {
if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) return i;
}
return -1;
}
のindexOfは大文字と小文字が区別されます。それは、リスト内の要素を比較するために、equalsメソッドを使用しているためです。同じことが含まれており、削除のために行く。