久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用多個不同的字符替換 String 中的多個字符

Replace multiple characters in String with multiple different characters(用多個不同的字符替換 String 中的多個字符)
本文介紹了用多個不同的字符替換 String 中的多個字符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在編寫一個代碼,它將二進(jìn)制數(shù)字轉(zhuǎn)換為相應(yīng)的單詞值.

I am working on a code that will convert binary digits to its corresponding value in words.

例如,我輸入3",代碼會將數(shù)字轉(zhuǎn)換為11",即3"的二進(jìn)制表示.代碼將繼續(xù)將11"轉(zhuǎn)換為one one",然后輸出.

For example, I would input "3" and the code will convert the number to "11", which is the binary representation of "3". The code will proceed to convert that "11" to "one one" which will be outputted.

我已經(jīng)寫了二進(jìn)制轉(zhuǎn)換部分,但是我很難將它轉(zhuǎn)換為單詞.

I have already wrote the binary conversion part, but I am having difficulty converting it to words.

public class BinaryWords {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String S = sc.nextLine(); //how many times the for loop will repeat
        for (int i = 0; i < S.length() + 1; i++) {
            int A = sc.nextInt(); //input the number
            String convert = Integer.toBinaryString(A); //converts the number to binary String
            String replace = convert.replaceAll("[1 0]", "one, zero "); //replaces the String to its value in words
            System.out.println(replace);
        }
    }
}

我嘗試將 replaceAll 函數(shù)與正則表達(dá)式 [1, 0] 一起使用,(我認(rèn)為)它將(兩者?)1 和 0 轉(zhuǎn)換為下一個字段中指定的序列.

I tried using the replaceAll function with the regex [1, 0], which (I think) will convert (both?) 1 and 0 to the sequence specified in the next field.

我想將每個 1 轉(zhuǎn)換為一",將每個 0 轉(zhuǎn)換為零".

I would like to convert every 1 to a "one" and every 0 to a "zero".

感謝任何幫助,謝謝!

推薦答案

你不需要使用正則表達(dá)式,你可以使用兩個替換來解決你的問題:

You dont need to use regex, you can use two replace to solve your problem :

String replace = convert.replace("1", "one ").replace("0", "zero ");

<小時>

例子:

int i = 55;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(i).replace("1", "one ").replace("0", "zero "));

輸出

110111
one one zero one one one 

<小時>

一年多后修改.


Edit after more than one year.

作為 @Soheil Pourbafrani 在評論中詢問,是否可以只遍歷字符串一次,是的,你可以,但你需要像這樣使用循環(huán):

As @Soheil Pourbafrani ask in comment, is that possible to traverse the string only one time, yes you can, but you need to use a loop like so :

int i = 55;
char[] zerosOnes = Integer.toBinaryString(i).toCharArray();
String result = "";
for (char c : zerosOnes) {
    if (c == '1') {
        result += "one ";
    } else {
        result += "zero ";
    }
}
System.out.println(result);
=>one one two one one one

Java 8+

如果您使用的是 Java 8+,或者更簡單,您可以使用:

Java 8+

Or more easier if you are using Java 8+ you can use :

int i = 55;
String result = Integer.toBinaryString(i).chars()
        .mapToObj(c -> (char) c == '1' ? "one" : "two")
        .collect(Collectors.joining(" "));
=>one one two one one one

這篇關(guān)于用多個不同的字符替換 String 中的多個字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 99这里只有精品视频 | 国产精品资源在线 | caoporn视频 | 日韩欧美成人一区二区三区 | av黄色免费在线观看 | 国产色爽 | 亚洲精品乱码久久久久久按摩观 | 黄色av网站在线观看 | 久久久精品 | 天天狠狠 | 羞羞视频网站免费观看 | 可以看黄的视频 | 亚洲国产欧美日韩 | 国产日韩欧美在线观看 | 国产精品99久久久久久大便 | www亚洲精品 | 久久艹免费视频 | 自拍视频网 | 日韩欧美在线视频 | 成人精品视频在线观看 | 久久精品欧美一区二区三区麻豆 | 一区二区不卡 | 日本精品在线观看 | 国产精品日女人 | 久草电影网 | 玖玖操 | 欧美精品久久久久久 | 一本大道久久a久久精二百 国产成人免费在线 | 免费久| 性高湖久久久久久久久aaaaa | 精品久久不卡 | 国产日韩精品视频 | 欧美在线观看一区 | 在线亚州 | 99精品欧美一区二区三区综合在线 | 国产999精品久久久久久 | 成年人在线播放 | 欧美 日韩 国产 成人 | 国内精品一区二区三区 | 国产精品视频网 | 国产精品美女久久久久久免费 |