![](/img/trans.png)
[英]From given integer value, how to print first two digit and then last two digit values in java
[英]How to take two digit from string in java?
如果用戶插入具有 ascii 代碼的字符串,例如
S="7289"
我想取兩位數的 72 並對其進行計算,然后對 89 進行計算並對其應用操作,我該怎么做..我想在這些操作轉換為它們的 ascii 代碼后? 比如 72=H 這是我的代碼(部分代碼)!
System.out.println("Enter CipherText : ");
String CipherText =scanner.next();
System.out.println("Using Private Key :(d,n) ("+d+","+n+")");
String ss="";
for(int i=0;i<CipherText.length();i++){
/*String sub =CipherText.substring(i, i++);*/
BigInteger bigIntValue1 = new BigInteger(CipherText);
String D= bigIntValue1.modPow(d,n).toString();
/*char ch1 = CipherText.charAt(i);
String strAscii = String.valueOf(ch1);*/
ss+=CipherText+" ";
}
System.out.println("Plain Text is :"+ss);
您可以使用substring
方法。
String str1 = str.substring(0, 2);
第一個參數是字符串的初始索引,第二個參數是要取字符的最后一個索引。
在其 2 個重載版本public String substring(int beginIndex)
和public String substring(int beginIndex, int endIndex)
使用方法 string.split
public static void main(String[] args) {
String S = "7289";
String highPart = S.substring(0, 2);
String lowPart = S.substring(2);
System.out.println(highPart + lowPart);
}
這也應該為您提供字符 72=H 或 H。
public class PrintASCIIChar {
public static void main(String[] args) {
String str = "7289";
String c = Character.toString((char)Integer.parseInt(str.substring(0, 2)));
System.out.println("Printing: " + c);
}
}
public static int findMaxNumber(String str) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < str.length() - 1; i++) {
String ss = str.substring(i, i + 2);
list.add(Integer.valueOf(ss));
Collections.sort(list);
}
int xx = list.get(list.size() - 1);
return xx;
}
System.out.println(TwoDigitFragmentNumber.findMaxNumber("50525"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.