如果我想将一个字符串按照每8位一组分为若干个块,然后存储在一个byte[ ]数组中,我首先需要确定这个byte数组的长度,但由于我无法确定这个字符串的长度是否可以被8整除,所以无法直接判断,因此需要对其进行取余,有下面两种方法可以解决问题

//声明一个需要切割的字符串
String str = "101000011011011010001100";

//方法一:
int len;
if (str.length() % 8 == 0){
len = str.length() / 8;
}else{
len = str.length() / 8 + 1;
}

//方法二:
int len = (str.length() + 7) % 8;

一行代码搞定,是不是很方便呢

String - 字符串分割操作的更多相关文章

  1. String字符串的操作

    字符串的常用操作 # Author:nadech name = "my name is nadech" print(name.count("a")) print ...

  2. string字符串js操作

    String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...

  3. String字符串相关操作

    .length 字符串长度.equals 比较字符串.equalIgnoreCase 比较字符串不区别大小写.charAt 获取字符串指定下标位置的字符.contains 判断字符串内是否包含某字符串 ...

  4. 4.String字符串类型操作

    String类型操作 1.set key value 设置key对应的值为string类型的value  2.mset key1 value1 … keyN valueN 一次设置多个key的值 3. ...

  5. string 字符串的操作 大全类的使用

    Array.Sort(vv, string.CompareOrdinal); //ASCII排序 string[] words = { "The", "1quick&qu ...

  6. mysql字符串分割操作

    SELECT '1,2,3,4,5,6,7,8' FROM dual; -- 列转行分割 ),) FROM () a JOIN mysql.help_topic b ); -- 测试 ),) as p ...

  7. std::string 字符串分割

    #include <iostream> #include <string> #include <vector> std::vector<std::string ...

  8. Java String 字符串操作小结

    // 转载加编辑 -- 21 Apr 2014 1. Java字符串中子串的查找 Java中字符串中子串的查找共有四种方法,如下: 1.int indexOf(String str) :返回第一次出现 ...

  9. 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。

    服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...

随机推荐

  1. python3练习100题——034

    题目:练习函数调用. 这个很容易了. def hello_world(): return "hello, world!" def fun(): print(hello_world( ...

  2. C++构造函数和重载函数运算符如何区分

    构造函数和重载函数运算符如何区分: class Distance { private: int feet; int inches; public: Distance(){ feet = ; inche ...

  3. markdown文本编辑学习笔记2

    目录 1.删除线 2.无序列表 4 todo list 5分割符号 6 TOC自动生成目录 7 插入代码块 8 斜体.粗体.删除线.下划线.背景高亮 markdown编辑公式 1.删除线 ~~要删除的 ...

  4. excel用xlrd日期变成42631.0

    datetime的解决办法混合数据的表中有个日期:2016/9/18 通过table.row_values(row_number)[1]读取时,显示的结果为:42631.0 查看row_values方 ...

  5. 如何在Word中排出漂亮的代码,去除回车符,去除拼写检查

    这位博主写到很到位,这里补充一下在VBA里用模块的部分. https://blog.csdn.net/code4101/article/details/41802715 1.放代码的方式是贴纯文本. ...

  6. t-SNE

    Don't look back. Don't hesitate, just do it. t-SNE原理 from here. 1. tsne is strictly used for visuali ...

  7. [Luogu]三步必杀

    Description Luogu4231 Solution 我最近到底怎么了,这种题都做不出来了,一看题第一反应李超线段树(虽然不会),觉得不可做,看一眼题解才发现这个题可以差分,然后差分还打错了好 ...

  8. Element-ui组件库Table表格导出Excel表格

    安装npm install --save xlsx file-saver 两个插件的详细地址在下面 https://github.com/SheetJS/js-xlsxhttps://github.c ...

  9. Python生成通用唯一识别码UUID

    from uuid import uuid4 for i in range(100): uid = str(uuid4()) suid = ''.join(uid.split('-')) print( ...

  10. pip知识点

    pip第三方模块保存在\Lib\site-packages目录 安装第三方模块:\Script目录下->shift+鼠标右点打开powershift窗口 ->pip install 模块名 ...