Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a function to intercept strings in java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "java how to write a string interception function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to write a string interception function" this article.

Java:

Public class StringSplit {public static void main (String [] args) throws Exception {String ss = "a very bc Hello"; System.out.println (splitString (ss, 1)) } public static String splitString (String str, int byteLength) throws Exception {/ / if the string is empty, return if directly (str = = null | | ".equals (str)) {return str } / / is used to count how many Chinese characters in this string int wordCount = 0; / / uniformly get its byte array according to gbk encoding, because different encoded byte arrays are different. Byte [] strBytes = str.getBytes ("GBK"); / / if only one bit is intercepted and the first bit is a Chinese character, if (byteLength = = 1) {if (strBytes [0] < 0) {return str.substring (0,1) A Chinese character in the}} / / string causes wordCount to add twice / / if what you take out of this byte is a Chinese character, that is, one of the two bytes, then the value of val is negative for (int I = 0; I < byteLength; iByte +) {int val = strBytes [I] If (val < 0) {wordCount++ }} / / if the number of intercepted digits passed is not truncated to more than half of the Chinese language, then intercept if (wordCount% 2 = = 0) {return str.substring (0, (byteLength-(wordCount / 2)) according to byteLength-(wordCount / 2). } / / otherwise, we will give up the extra one, so-1 return str.substring (0, (byteLength-(wordCount / 2)-1));}}

Js

Function isChinese (str) {/ / determine whether it is Chinese var reCh=/ [u00-uff] /; return! reCh.test (str);} function lenStat (target) {var strlen=0; / / the initial definition length is 0 var txtval = $.trim (target.val ()); for (var iTuno)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report