/*
* Copyright 2023 tu.cn All right reserved. This software is the
* confidential and proprietary information of tu.cn ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Tu.cn
*/
package com.audaque.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
/**
* @author DELL
* @date 2023/11/20 - 14:55
*/
public class FullAngleToHalfAngle extends UDF {
public Text evaluate(Text s){
/*如果字段值为null,直接返回空值不做校验*/
if (s == null) {
return null;
}
String str = s.toString();
StringBuffer result = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
/*全角的ascii码范围*/
if ( c >= 65281 && c <= 65374 ){
c = (char) (c-65248);
/*空格的全角转半角需要单独判定*/
}else if (c == 12288){
c = (char) 32;
}
result.append(c);
}
return new Text(result.toString());
}
public static void main(String[] args) {
FullAngleToHalfAngle fa = new FullAngleToHalfAngle();
System.out.println(fa.evaluate(new Text("我我asaddasd54351531312")));
System.out.println("我我asaddasd54351531312");
System.out.println((int)(char) ' ');
System.out.println((int)(char) ' ');
System.out.println(fa.evaluate(new Text(" ")));
System.out.println((char)65280);
System.out.println((char) (65280-65248));
System.out.println((char)65281);
System.out.println((char) (65281-65248));
}
}