LoginSignup
3
2

More than 5 years have passed since last update.

目的

テキストエリアから入力された文字列を、
適切な処理をして問題なくDBに格納をする。

実装のために考えたこと

  • javascriptのスクリプトタグのエスケープ
  • サロゲートペアの文字列の文字数カウントの変更

ソースコード

スクリプトタグのエスケープ処理

public static String removeTag(String inputedText) {
    String text = inputedText;
    if (text != null) {
        text = text.replaceAll("<", "&lt;");
        text = text.replaceAll(">", "&gt;");
    }
    return text;
}

サロゲートペアの文字列チェックの変更

サロゲートペアの文字列も1文字として扱いたいため。

public static void hasCorrectTextLength(String tweetText) {
    BreakIterator bi = BreakIterator.getCharacterInstance();
    bi.setText(tweetText);
    int textCount = 0;
    while (bi.next() != BreakIterator.DONE) {
        textCount++;
    }
    if (textCount > 140) {
        throw new ActionMessagesException("ツイートの長さが最大値(140)を超えています。", false);
    }
}

改善点

BreakIterator

はほとんど見たことがないとの評価を受けた。
実装をする際には、一度どういったメソッドがはいっているのか調べておいた方がいいかもしれない。

3
2
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
3
2