1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

String#toLowerCase()でハマりました

Posted at

いまさらなのかもしれないですがString#toLowerCase()は実行環境のロケール依存なのでちゃんと引数にLocale.ENGLISHを渡してあげると思った通りの結果が出ますよという話です。日本語環境だけで動かすと気づかないですよね…

testTr()だけfailedになります。トルコ語環境以外でも起きるかもしれません

package example;

import static org.junit.Assert.*;

import java.util.Locale;

import org.junit.Test;

public class LocaleTestCase {

	@Test
	public void testJa() {
		Locale.setDefault(Locale.JAPANESE);
		String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String lower = "abcdefghijklmnopqrstuvwxyz";
		assertEquals(lower, upper.toLowerCase());
	}

	@Test
	public void testTr() {
		Locale tr = new Locale("tr");
		Locale.setDefault(tr);
		String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String lower = "abcdefghijklmnopqrstuvwxyz";
		// iが一致しない
		assertEquals(lower, upper.toLowerCase());
	}

	@Test
	public void testTr2() {
		Locale tr = new Locale("tr");
		Locale.setDefault(tr);
		String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String lower = "abcdefghijklmnopqrstuvwxyz";
		assertEquals(lower, upper.toLowerCase(Locale.ENGLISH));
	}
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?