パターン①
midで1文字ずつ分割してチェックする
public static String biteCheck1 (String targetStr, Integer maxLength) {
List<String> resultList = new List<String>();
String result = '';
String str = '';
Integer j = 0;
for (Integer i = 0; i < targetStr.length(); i++) {
str = targetStr.mid(i,1);
j += Blob.valueOf(str).size();
if (j <= maxLength) {
resultList.add(str);
} else {
break;
}
}
for (String s : resultList) result += s;
system.debug(Blob.valueOf(result).size());
system.debug(result);
return result;
}
パターン②
split('')で分割してチェックする
public static String biteCheck2 (String targetStr, Integer maxLength) {
List<String> splitList = targetStr.split('');
String result = '';
List<String> resultList = new List<String>();
Integer j = 0;
for (String s : splitList) {
j += Blob.valueOf(s).size();
if (j <= maxLength) {
resultList.add(s);
} else {
break;
}
}
for (String s : resultList) result += s;
system.debug(Blob.valueOf(result).size());
system.debug(result);
return result;
}