简体   繁体   中英

How to convert a mix string of normal English , Russian(Cyrillic) and encoded Russian string into normal string by using java

Lits explain the problem, I have a pattern of string from a third party API call. I am just giving the example of the string what that API return. ex -

Статья 1 Все люди рождаются свободными и равными в своем достоинстве и правах. Они наделены разумом и совестью и должны поступать в отношении друг друга в духе братства,

Article 1 All people are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards each other in a spirit of brotherhood,

анализы давид маша ЛЕОÐТЬЕВРМÐРИЯ ЕГОРОВÐÐ
_терапевт_Леонтьева Вод.правÐ

This above type of mix string(it's just a prototype) I am getting from calling of a third-party API, some part of the string is in Russian and some part is in English and some part is encrypted. But when I decrypt or convert it into UTF-8 type character it's not working. The solution of this problem should be like this

Статья 1 Все люди рождаются свободными и равными в своем достоинстве и правах. Они наделены разумом и совестью и должны поступать в отношении друг друга в духе братства,

Article 1 All people are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards each other in a spirit of brotherhood,

анализ давид маа НТЬА А Я  НА
_еапев_еонева од.пав

Can we solve it by using JAVA?

You need to inspect the text character by character, determine if the character is "normal" or an encryption code.

StringBuilder output= new StringBuilder();
char[] chars = userText.toCharArray();

for (int index = 0; index < chars.length; index++)
{
    if ( isEncryption( chars[index] ) )
        output.append( decrypt( chars[index] ) );
    else
        output.append( chars[index] );
}

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

If the encryption is multi-character, then it gets a little more complex

StringBuilder output= new StringBuilder();
StringBuilder encrypted= new StringBuilder();
boolean isInEncrypt=false;

char[] chars = userText.toCharArray();

for (int index = 0; index < chars.length; index++)
{
    if ( isEncryption( chars[index] ) )
        {
            isInEncrypt=true;
            encrypted.append( chars[index] );
        }
    else
        {
            if ( isInEncrypt )
            {
                output.append( decrypt( encrypted.toString() ) );
                encrypted.setLength(0);
                isInEncrypt=false;
            }
            else
               output.append( chars[index] );
        }
}

if ( isInEncrypt  && encrypted.length() > 0 ) 
  output.append( decrypt( encrypted.toString() ) );
              
System.out.println(output.toString()); 
     

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