简体   繁体   English

我如何获得 output 之类的 output 字符串?

[英]How I get the output like output string?

I am trying to solve this issue in so many times.我试图解决这个问题很多次。 But I can't understand how to get numeric value respected ASCII character.但我不明白如何获得数值尊重的 ASCII 字符。

By following this steps:按照以下步骤操作:

  1. Replace the character in even position with their corresponding ASCII value将 position 中的字符替换为其对应的 ASCII 值
  2. Reverse the string using StringBuilder.使用 StringBuilder 反转字符串。

Input string:--->输入字符串:--->

?85O89*69R65*87O104*33I104

Output should be like this:---> Output应该是这样的:--->

?uoy*era*woH*!iH

How I get the output In java?我如何在 java 中获得 output?

Unlike the others I think this is actually an interesting question, that a newbie may find difficult to understand.与其他人不同,我认为这实际上是一个有趣的问题,新手可能会觉得难以理解。

Given the string给定字符串

?85O89*69R65*87O104*33I104

We see there is repetitive pattern, a character + a numeric value.我们看到有重复的模式,一个字符+一个数值。 We have then那时我们有

?85
O89
*69
R65
*87
O104
*33
I104  

Based on the proposed output we understand the number represents an ASCII codepoint .基于提议的 output 我们理解数字代表一个ASCII 码点
Also we see that the case of each character is reversed.我们还看到每个字符的大小写都颠倒了。

Thus, we may write the following code.因此,我们可以编写以下代码。

final String value = "?85O89*69R65*87O104*33I104";
final StringBuilder sb = new StringBuilder();

// We split the string based on the \w(\d+) pattern, "reversed"
final String[] parts = value.split("(?=([^\\d]+))");

for (final String s : parts) {
   // For each character we must reverse its case
   // The first symbol is already in a presentable form
   final char c = s.charAt(0);
   sb.append(Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c));

   // The remaining part represents an ASCII symbol in decimal form
   final int codePoint = Integer.parseInt(s.substring(1));
   final String str = new String(Character.toChars(codePoint));
   sb.append(Character.isUpperCase(codePoint) ? str.toLowerCase() : str.toUpperCase());
}

System.out.println(sb);

Which will correctly print哪个会正确打印

?uoy*era*woH*!iH

The input string seems to be using * as "word" delimiters which should be kept in the output.输入字符串似乎使用*作为“单词”分隔符,应保留在 output 中。

A "word" contains 3 characters, the first and the last characters are displayed as ASCII codes in the encoded string.一个“单词”包含 3 个字符,第一个和最后一个字符在编码字符串中显示为 ASCII 码。

When decoding a word, the case of the letter character has to be inverted: lower -> upper, upper -> lower.解码一个单词时,字母字符的大小写必须反转:lower -> upper,upper -> lower。

That being said, the following implementation may be as follows:话虽如此,以下实现可能如下:

static Pattern WORD = Pattern.compile("(\\d+)(.)(\\d+)");

public static String decode(String str) {
    return Arrays
        .stream(str.split("((?<=[*!\\?])|(?=[*!\\?]))"))
        .map(s -> s.matches(WORD.pattern()) ? decodeWord(s) : s)
        .collect(Collectors.joining());
}

private static String decodeWord(String word) {
    Matcher match = WORD.matcher(word);
    if (match.find()) {
        return new StringBuilder()
            .append(decodeChar((char)(Integer.parseInt(match.group(1)))))
            .append(decodeChar(match.group(2).charAt(0)))
            .append(decodeChar((char)(Integer.parseInt(match.group(3)))))
            .toString();
    }
    return ""; 
}

static char decodeChar(char c) {
    if (Character.isLetter(c)) {
        return Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
    }
    return c;
}

Test:测试:

System.out.println(decode("?85O89*69R65*87O104*33I104"));

Output: Output:

?uoy*era*woH*!iH

According to what you said, and base on the string you give, I'm ASSUMING that every even position is between(not include the ending) two non-numbers.根据您所说的,并根据您给出的字符串,我假设每个 position 都介于(不包括结尾)两个非数字之间。

    public static String perform(String str) {
        StringBuilder sb = new StringBuilder("");
        
        String dump = "";//dump string
        for(int i = 0;i<str.length();i++) {//Read the string char by char
            int ascii = (int) str.charAt(i);//work with ASCII
            
            if(ascii - '0' < 0 || ascii - '0' > 9) {//check if ascii is a non-number
                
                if(dump.length() != 0) {//can dump
                    int c = Integer.parseInt(dump);
                    
                    if(c >= 65 && c <= 90) {//uppercase to lowercase
                        sb.append((char) (c - 'A' + 'a'));
                    }
                    
                    if(c >= 97 && c <= 122) {//lowercase to uppercase
                        sb.append((char) (c - 'a' + 'A'));
                    }
                    dump = "";//clear
                }
                
                if(ascii < 65 || (ascii > 90 && ascii < 97) || ascii > 122) {//not a letter
                    sb.append(str.charAt(i));
                }else {//is a letter
                    if(ascii >= 65 && ascii <= 90) {//uppercase to lowercase
                        sb.append((char) (ascii - 'A' + 'a'));
                    }
                    
                    if(ascii >= 97 && ascii <= 122) {//lowercase to uppercase
                        sb.append((char) (ascii - 'a' + 'A'));
                    }
                }
                
            }else {// Is number
                dump += str.charAt(i);
            }
        }
        
        if(dump.length() != 0) {
            int c = Integer.parseInt(dump);
            
            if(c >= 65 && c <= 90) {//uppercase to lowercase
                sb.append((char) (c - 'A' + 'a'));
            }
            
            if(c >= 97 && c <= 122) {//lowercase to uppercase
                sb.append((char) (c - 'a' + 'A'));
            }
        }
        
        return sb.toString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM