【问题标题】:How to mix the processing of a Java string如何混合处理一个Java字符串
【发布时间】:2014-06-04 16:54:13
【问题描述】:

Java 字符串包含两种类型的单词。 第一个类型的词是:

Wx1,Wx2,Wx3,Wx4,Wx5,Wx6, Wx7 etc.

第二种类型的词是:

Wy1,Wy2 etc. 

给定

String str= "Wx1 Wx2   Wy1   Wx3 Wx4   Wy2    Wx5 Wx7 Wx8"

第一种由function1()处理,第二种由function2()处理,function3()进一步处理function1()的级联输出。

function1(Wx1) gives String S1
function1(Wx2) gives String S2
String S12= S1 + S2;
    function3(S12) returns String str1


function2(Wy1) gives String Y1

function1(Wx3) gives String S3
function1(Wx4) gives String S4

String S34= S3 + S4;
    function3(S34) returns String str2

    function2(Wy2) gives String Y2

function1(Wx5) gives String S5
function1(Wx6) gives String S6
function1(Wx7) gives String S7
function1(Wx8) gives String S8

String S5678 = S5 + S6 + S7 + S8
    function3(S5678) returns String str3


String output = str1 + Y1+ str2 + Y2 + str3;

This is the whole logic my program.

我在最后一步很困惑,以塑造在哪里以及如何调用 function3() 的循环:

for(int i=0; i<Words.length; i++){

if Word[i]= Type1??????????{ doThis(); ?????

//somewhere here I have to call function3(), how the program will judge that the next word is of type2, hence function3 is to be called here.// WHERE AND HOW TO CALL function3();
    }
else {doThat();
}

作为编程新手,我需要帮助来形成循环的逻辑。

【问题讨论】:

  • 您正在寻找模式
  • type 1 words 和 type 2 words 有什么区别?当您可以用英语或您的母语回答该问题时,您就可以将您的需求转化为代码。
  • 第一种以 1h,2h,5k 等数字开头,第二种以 fsh,ggk,nas,ilk 等字母开头。

标签: java string


【解决方案1】:

我对字符串 S12、S34 等的多重创建感到困惑。最后你的结果应该是S12 + Y1+ S34 + Y2 + S5678;。这相当于S1 + S2 + Y1 + S3 + S4 + Y2 + S5 + S6 + S7 + S8。那么在这里创建这些中间连接有什么意义吗?如果没有,你可以试试这个程序。

编辑:

public class MixProcessor {
    private static int counter = 0;

    public static void main(String[] args) {
        String str= "Wx1 Wx2 Wy1 Wx3 Wx4 Wy2 Wx5 Wx7 Wx8";
        StringBuilder output = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        String previousPatter = "";
        boolean shouldMerge = false;

        for(String s : str.split(" ")){
            String processedString = "";

            if (s.matches(".*?x.*")){
                shouldMerge = ("x".equals(previousPatter) || previousPatter.trim().length() == 0) ? true : false;
                previousPatter = "x";
                processedString = function1(s);
            } else if (s.matches(".*?y.*")){
                shouldMerge = ("y".equals(previousPatter) || previousPatter.trim().length() == 0) ? true : false;
                previousPatter = "y";
                processedString = function2(s);
            }

            if(shouldMerge){
                temp.append(processedString);
            } else {
                output.append(function3(temp.toString()));
                temp = new StringBuilder(processedString);
            }
        }
        //Append the last value
        output.append(function3(temp.toString()));

        System.out.println(output.toString());
    }

    private static String function2(String s) {
        return "Y" + s.substring(s.indexOf("y") + 1);
    }

    private static String function1(String s) {
        return "S" + s.substring(s.indexOf("x") + 1);
    }

    private static String function3(String s) {
        // Here this could be any function. I'm just trying to make as per the question
        return (s.indexOf("S") != -1) ? ("str" + (++counter)) : s;
    }

}

【讨论】:

  • 你的解决方案让我明白了很多。我已经编辑了我的问题并包含了 function3() 以清除我对字符串的“多重创建”并完善我的需求的解释。现在我期待您提供更合适的解决方案,请这样做。
  • 使用一些布尔值和 if-else 应该很容易。更新了我的答案。可能会有所帮助。 :)
  • 答案已更新,但我真的需要您的进一步帮助。
  • 当然。告诉我。您还需要什么帮助?
  • 您的最新指南非常完美。如果我在实施中发现任何困难,我会请求你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 2019-03-20
相关资源
最近更新 更多