简体   繁体   中英

How to take two digit from string in java?

If user insert string that have ascii code numbers such as

S="7289" 

I want to take two digit 72 and make computations on it then and 89 and apply operations on it , how i can do it .. and i want after these operations convert to their ascii code ? such as 72=H This is my code (part of the code) !

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);

You can use the substring method.

String str1 = str.substring(0, 2);

First parameter is the initial index of the string, second parameter the last index of the characters you want to take.

Use the method string.split in its 2 overloaded versions public String substring(int beginIndex) and public String substring(int beginIndex, int endIndex)

their respective docs are here and here

Example:

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);
}

This should also give you the char 72=H or 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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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