如何将字符串中的字母编码为数字

我想做的是将字符串中的数字根据字母表编码为数字,并保留数字不变。所以“abc123”将是“123123”。在 javascript 中找到了解决方案,但似乎无法适应 java。任何帮助都会很棒,谢谢。


java 函数会是这样的


import java.util.*; 

import java.io.*;


class Main {


  public static String NumberEncoding(String str) {

    ***call javascript function or translate it into java

  }


  public static void main (String[] args) {  

    // keep this function call here     

    Scanner s = new Scanner(System.in);

    System.out.print(NumberEncoding(s.nextLine())); 

  }


}

jasvascript 函数是


function NumberEncoding(str) { 

str = str.toLowerCase();

var obj = {};

var alpha = "abcdefghijklmnopqrstuvwxyz";

var result = "";

for (var i = 1; i <= alpha.length; i++) {

    obj[alpha[i-1]] = i;

}


for (var j = 0; j < str.length; j++) {

  if (str[j].match(/[a-z]/)) {

    result += obj[str[j]];

  } else {

    result += str[j]; 

  }

}

return result;

}


烙印99
浏览 85回答 3
3回答

手掌心

第一步,创建一个变量来累加String结果;我会用一个StringBuilder. 第二步,String一次一个字符地迭代输入。第三步,将该字符转换为小写。第四步,检查该字符是否不是数字。第五步,如果字符是数字,则将其直接传递,否则该值很容易确定,因为 Java 字符是整数类型(例如 'a' + 1 = 'b' 和 'b' - 1 = 'a')。第六步,将结果作为String. 最后,Java 命名约定是驼峰命名法(以小写字母开头)。喜欢,public static String encodeNumber(String str) {&nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; for (int j = 0; j < str.length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; char c = Character.toLowerCase(str.charAt(j));&nbsp; &nbsp; &nbsp; &nbsp; if (c < 'a' || c > 'z') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(c);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(1 + c - 'a');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result.toString();}但是,如果您确实愿意,您确实可以使用Nashorn从 Java 直接调用 JavaScript 函数。喜欢,String f = "function NumberEncoding(str) { str = str.toLowerCase();\n"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; + "var obj = {};\n"&nbsp; &nbsp; &nbsp; &nbsp; + "var alpha = \"abcdefghijklmnopqrstuvwxyz\";\n"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; + "var result = \"\";\n"&nbsp; &nbsp; &nbsp; &nbsp; + "for (var i = 1; i <= alpha.length; i++) {\n"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; obj[alpha[i-1]] = i;\n" + "}\n" + "\n"&nbsp; &nbsp; &nbsp; &nbsp; + "for (var j = 0; j < str.length; j++) {\n"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; if (str[j].match(/[a-z]/)) {\n"&nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; result += obj[str[j]];\n"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; } else {\n" + "&nbsp; &nbsp; result += str[j];" + "&nbsp; }\n" + "}\n"&nbsp; &nbsp; &nbsp; &nbsp; + "return result;\n" + "}";ScriptEngine se = new ScriptEngineManager().getEngineByName("js");try {&nbsp; &nbsp; se.eval(f);&nbsp; &nbsp; Invocable invocable = (Invocable) se;&nbsp; &nbsp; Object result = invocable.invokeFunction("NumberEncoding", "zabc123");&nbsp; &nbsp; System.out.println(result);} catch (Exception e) {&nbsp; &nbsp; e.printStackTrace();}为了同样的结果。

青春有我

一种方法是使用 StringBuilder。&nbsp; &nbsp; &nbsp; List<String> strings = Arrays.asList("abc123", "e2f3g4");&nbsp; &nbsp; &nbsp; for (String s : strings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringBuilder sb = new StringBuilder(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i < sb.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char c = sb.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Character.isAlphabetic(c)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sb.replace(i, i + 1, Integer.toString(c - 'a' + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(sb.toString());&nbsp; &nbsp; &nbsp; }还有 Stream 版本。&nbsp; &nbsp; &nbsp; List<String> strings = Arrays.asList("123abc", "e1f2g3", "xyz123");&nbsp; &nbsp; &nbsp; List<String> converted = strings.stream().map(str -> str.chars().map(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chr -> Character.isAlphabetic(chr) ? chr - 'a' + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : chr - '0').mapToObj(String::valueOf).collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.joining())).collect(Collectors.toList());&nbsp; &nbsp; &nbsp; System.out.println(converted);

天涯尽头无女友

解决步骤可以是用值 1 到 26填充int[]大小为 26(字母表数)的值,对应于字母表中字母的位置。遍历输入字符串的所有字符并将其位置从 附加int[]到 a&nbsp;StringBuilder。如果该字符不是字母表,则按原样追加。演示:public class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(numberEncoding("abc123"));// Expected: 123123&nbsp; &nbsp; }&nbsp; &nbsp; static String numberEncoding(String str) {&nbsp; &nbsp; &nbsp; &nbsp; str = str.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; String alpha = "abcdefghijklmnopqrstuvwxyz";&nbsp; &nbsp; &nbsp; &nbsp; int[] obj = new int[alpha.length()];&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder result = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= obj.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[i - 1] = i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < str.length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (str.charAt(j) >= 'a' && str.charAt(j) <= 'z') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(String.valueOf(obj[j]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append(str.charAt(j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result.toString();&nbsp; &nbsp; }}输出:123123
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java