導航:首頁 > 編程語言 > byte截取java

byte截取java

發布時間:2022-08-05 15:23:42

java 按位元組截取字元串問題

public class FormatTool {
/**
*
*
* @param
* @return String
* @param formatStr
* 被格式化字元串
* @param tag
* 不足位補充字元串
* @param len
* 字元串長度
* @param direction
* 1:左補,0:右補
* @return desc
*/
public static String format(Object formatStr, String tag, int len,
int direction) {
String str = formatStr.toString();
if (len <= str.length()) {
return str.substring(0, len);
}
StringBuilder tempStr = new StringBuilder();
for (int i = 0; i < len - str.getBytes().length; i++) {
tempStr.append(tag);
}
if (direction == 0) {
return str + tempStr;
} else {
return tempStr.append(formatStr).toString();
}
}

/**
* 位元組數組拷貝
*
* @param
* @return void
* @param fromBytes
* @param toBytes
* @param from
* @param len
* desc
*/
public static void cpyBytes(byte[] fromBytes, byte[] toBytes, int from,
int len) {
for (int i = from; i < from + len; i++) {
toBytes[i - from] = fromBytes[i];
}
}

/**
* 獲取字元串formatStr從from到from + len的字元串
*
* @param
* @return String
* @param formatStr
* @param from
* @param len
* @return
* desc
*/
public static String format(String formatStr, int from, int len) {
byte[] fromBytes = formatStr.getBytes();
byte[] toBytes = new byte[len];
cpyBytes(fromBytes, toBytes, from, len);
return new String(toBytes);
}
}
使用方法FormatTool.format(str,50,400);表示的意思就是截取str字元串從第50個位元組開始截取,截取400個位元組的字元串

㈡ java截取字元串函數

這個程序是通過先把字元串轉換為byte 數組然後根據你要截取的字元串長度截取字元串的。
byte bt[] = str.getBytes(); //轉化為位元組數組。
if(bt[byteNum]<0)//判斷截取的長度,截取的長度不能是否小於0
{
String substrx=new String(bt,0,--byteNum);
/*String(byte[] ascii, int hibyte)
已過時。 該方法無法將位元組正確轉換為字元。從 JDK 1.1 起,完成該轉換的首選方法是通過 String 構造方法,該方法接受一個字元集名稱或使用平台的默認字元集。*/

//建議: 學習的時候多看看API

㈢ JAVA的byte類型如何截斷

因為java裡面byte的范圍是-128到127,由於byte b3 = (byte)(b1+b2)裡面有強制byte轉換,所以如果結果mod 256超過127的話,計算的時候按照 -128+(b1+b2)%128計算

㈣ java將byte數組中的中間一部分值取出來怎麼做啊

如果以這種方式存儲,那麼一定是定長字元串,byte[]是以位元組來存儲的,你直接取規則的長度就行了啊
如下:
byte[] b = new byte[10];
b[0]='a';
b[1]='b';
b[2]='c';
b[3]='d';
String a = new String(b,0,2);
用你的例子來說:比如你的標志是5位的,編號12位,日期20位,測量值10位
那麼應該是
String bz = new String(b,0,5);
String bh = new String(b,5,12);
...............
以此方式解析

㈤ java字元串位元組長度截取問題

contentSummanry = contentSummanry.substring(0,100); 這就是說我要截取前面一百個字元

同學 這是最好的截取了 你試試
contentSummanry就是你要去裡面截取的字元串 看看這里吧:
public class CutString {

/**
* 判斷是否是一個中文漢字
*
* @param c
* 字元
* @return true表示是中文漢字,false表示是英文字母
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的編碼格式
*/
public static boolean isChineseChar(char c)
throws UnsupportedEncodingException {
// 如果位元組數大於1,是漢字
// 以這種方式區別英文字母和中文漢字並不是十分嚴謹,但在這個題目中,這樣判斷已經足夠了
return String.valueOf(c).getBytes("GBK").length > 1;
}

/**
* 按位元組截取字元串
*
* @param orignal
* 原始字元串
* @param count
* 截取位數
* @return 截取後的字元串
* @throws UnsupportedEncodingException
* 使用了JAVA不支持的編碼格式
*/
public static String substring(String orignal, int count)
throws UnsupportedEncodingException {
// 原始字元不為null,也不是空字元串
if (orignal != null && !"".equals(orignal)) {
// 將原始字元串轉換為GBK編碼格式
orignal = new String(orignal.getBytes(), "GBK");
// 要截取的位元組數大於0,且小於原始字元串的位元組數
if (count > 0 && count < orignal.getBytes("GBK").length) {
StringBuffer buff = new StringBuffer();
char c;
for (int i = 0; i < count; i++) {
// charAt(int index)也是按照字元來分解字元串的
c = orignal.charAt(i);
buff.append(c);
if (CutString.isChineseChar(c)) {
// 遇到中文漢字,截取位元組總數減1
--count;
}
}
return buff.toString();
}
}
return orignal;
}

public static void main(String[] args) {
// 原始字元串
String s = "我ZWR愛JAVA";
System.out.println("原始字元串:" + s);
try {
System.out.println("截取前1位:" + CutString.substring(s, 1));
System.out.println("截取前2位:" + CutString.substring(s, 2));
System.out.println("截取前4位:" + CutString.substring(s, 4));
System.out.println("截取前6位:" + CutString.substring(s, 6));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

㈥ 【急求】java 按位元組截取字元串,從指定的位置開始,到指定的位置結束

用位元組的話可以這樣
String a = "ab我愛你cd";
byte[] bs = a.getBytes();
a = new String(bs,3,6);
這樣a這個字元串就是 我愛你 的字元了

㈦ java中怎麼進行byte位元組長度截取

substring(int,int)

㈧ 如何截取 byte數組

例:

import java.io.UnsupportedEncodingException;

public class CustString {
public static void main(String[] args)
throws UnsupportedEncodingException {
String s =
"我ZWR愛JAVA";
// 獲取GBK編碼下的位元組數據
byte[] data =
s.getBytes(「GBK」);
byte[] tmp = new byte[6];
// //
將data數組的前六個位元組拷貝到tmp數組中
System.array(data, 0, tmp, 0, 6);
//
// 將截取到的前六個位元組以字元串形式輸出到控制台
s = new
String(tmp);
System.out.println(s);
}
}

結果:

��ZWR�

如果去掉GBK,則結果為:我ZWR

方法論2:

import java.io.UnsupportedEncodingException;

public class CutString {

public static boolean
isChineseChar(char c)
throws
UnsupportedEncodingException { // 如果位元組數大於1,是漢字 //
//
以這種方式區別英文字母和中文漢字並不是十分嚴謹,但在這個題目中,這樣判斷已經足夠了
return
String.valueOf(c).getBytes().length > 1;
}

public static String substring(String orignal, int
count)
throws UnsupportedEncodingException { //
原始字元不為null,也不是空字元串
if (orignal != null &&
!"".equals(orignal)) { // 將原始字元串轉換為GBK編碼格式
orignal = new
String(orignal.getBytes()); // 要截取的位元組數大於0,且小於原始字元串的位元組數
if
(count > 0 && count < orignal.getBytes().length)
{
StringBuffer buff = new
StringBuffer();
char
c;
for (int i = 0; i < count; i++)
{
// charAt(int
index)也是按照字元來分解字元串的
c =
orignal.charAt(i);
buff.append(c);
if
(CutString.isChineseChar(c)) { //
遇到中文漢字,截取位元組總數減1
--count;
}
}
return
buff.toString();
}
}
return
orignal;
}

public static void main(String[] args) { // 原始字元串
String
s = "我ZWR愛JAVA";
System.out.println("原始字元串:" +
s);
try {
System.out.println("截取前1位:" +
CutString.substring(s, 1));
System.out.println("截取前2位:" +
CutString.substring(s, 2));
System.out.println("截取前4位:" +
CutString.substring(s, 4));
System.out.println("截取前6位:" +
CutString.substring(s, 6));
} catch
(UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
}
即判斷是否為漢字,再截取

㈨ java認證:如何按位元組長度截取字元串

編程:編寫一個截取字元串的函數,輸入為一個字元串和位元組數,輸出為按位元組截取的字元串。public static String substring(String str, int toCount,String more){int reInt = 0;String reStr = 「」;if (str == null)return 「」;char[] tempChar = str.toCharArray();for (int kk = 0; (kk 《 tempChar.length && toCount 》 reInt); kk++) {String s1 = str.valueOf(tempChar[kk]);byte[] b = s1.getBytes();reInt += b.length;reStr += tempChar[kk];}if (toCount == reInt || (toCount == reInt - 1))reStr += more;return reStr;}Web應用程序在瀏覽器中顯示字元串時,由於顯示長度的限制,常常需要將字元串截取後再進行顯示。但目前很多流行的語言,如C#、Java內部採用的都是 Unicode 16(UCS2)編碼,在這種編碼中所有的字元都是兩個字元,因此,如果要截取的字元串是中、英文、數字混合的,就會產生問題,如下面的字元串:String s = 「a加b等於c,如果a等1、b等於2,那麼c等3」;上面的字元串既有漢字,又有英文字元和數字。要解決這個問題的方法是首先得到該字元串的UCS2編碼的位元組數組,如下面的代碼如下:byte[] bytes = s.getBytes(」Unicode「);由於上面生成的位元組數組中前兩個位元組是標志位,bytes[0] = -2,bytes[1] = -1,因此,要從第三個位元組開始掃描,對於一個英文或數字字元,UCS2編碼的第二個位元組是相應的ASCII,第一個位元組是0,如a的UCS2編碼是0 97,而漢字兩個位元組都不為0,因此,可以利於UCS2編碼的這個規則來計算實際的位元組數,

㈩ java,要實現字元串按位元組截取的方法

這個不能用char數組的,因為char的范圍是-128~128,漢字一般大於127的,我提供一個方案,將string轉成byte數組,可以跟編碼方式,如果是gbk就是兩個一組,utf-8是3個一組,遍歷數組,使用邏輯與&128如果不為0表示漢字,就按編發方式三個或兩個一組,如果為0就是普通的iso-8859-1,也就是一個位元組一個字元,這樣問題就解決了,希望對你有幫助

閱讀全文

與byte截取java相關的資料

熱點內容
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745
html實例教程pdf 瀏覽:157
linux命令開放所有許可權 瀏覽:575
30歲能學會編程 瀏覽:737
小火箭的伺服器是什麼 瀏覽:967
cad查信息命令 瀏覽:402