相关疑难解决方法(0)

从Java中的字符串中获取第一个字母的最佳方法是什么,以长度为1的字符串返回?

假设如下:

String example      = "something";
String firstLetter  = "";
Run Code Online (Sandbox Code Playgroud)

是否存在差异,需要注意以下firstLetter可能影响绩效的分配方式; 哪个最好,为什么?

firstLetter = String.valueOf(example.charAt(0));
firstLetter = Character.toString(example.charAt(0));
firstLetter = example.substring(0, 1);
Run Code Online (Sandbox Code Playgroud)

第一个字母作为a返回的原因String是它在Hadoop中运行,并且需要分配给Text类型的字符串,firstLetter将作为a keymap()方法输出,例如:

public class FirstLetterMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    String line = new String();
    Text firstLetter = new Text();
    IntWritable wordLength = new IntWritable();

    @Override
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        line = value.toString();

        for (String word : line.split("\\W+")){
            if …
Run Code Online (Sandbox Code Playgroud)

java string

67
推荐指数
3
解决办法
23万
查看次数

如何列出字符串中的所有提及

我正在创建一个EditText可以在用你的昵称写 @ 时提及其他用户的内容,例如在 Facebook、Instagram、Twitter 等上。例如,在文本“今天@john 和@jane 吃了蛋糕”中,我想创建一个ArrayList与数值“约翰”和“简”。我试过这个,但没有用。该怎么做才能做到这一点?

java android kotlin

2
推荐指数
1
解决办法
304
查看次数

标签 统计

java ×2

android ×1

kotlin ×1

string ×1