简体   繁体   English

Java使字符串简写/不允许使用replace()函数/静态方法

[英]Java Making a string shorthand/No replace() function allowed/static method

I have been having trouble with a certain code problem, where I have to Create a method that receives a String and returns the String converted into shorthand. 我在某些代码问题上遇到了麻烦,我必须创建一个方法来接收一个String并将返回的String转换为简写形式。 I'm not allowed to use the replace functions and I don't really know how to use arrays yet. 我不允许使用替换功能,而且我还真的不知道如何使用数组。

I have to fit according to these standards: 我必须符合以下标准:

A.replace these four words: "and" with "&", "to" with "2", "you" with "u", and "for" with "4" whenever they appear as separate words within a sentence. 答:只要它们在句子中作为单独的单词出现,就用这四个词替换:“和”和“&”,“到”和“ 2”,“ you”和“ u”以及“ for”和“ 4”。

B.remove all vowels ('a', 'e', 'i', 'o', 'u'), whether lowercase or uppercase, unless the vowel appears by itself. B.删除所有小写或大写的元音(“ a”,“ e”,“ i”,“ o”,“ u”),除非元音单独出现。

For example, For I shall love you forever becomes 4 I shll lv u frvr 例如, For I shall love you forever变成4 I shll lv u frvr

here is my code in progress, I haven't necessarily started since I don't know how to approach the problem. 这是我的代码在进行中,因为我不知道如何解决该问题,所以不一定要开始。

public static String shortHand(String str){
        /* strategy: 
         * take string length. do a for loop for each individual letter and then search for letters and, replace
         * with &, look for to, replace with 2, replace you w/ u, and then take out any vowels.
         */ 

      for(int i = str.length(); i < str.length(); i++){

           str.toLowerCase();


        }
       return "";
    }

It would be helpful if I could find a solution that utilizes helper methods, but not mandatory. 如果我可以找到一个使用辅助方法但不是强制性的解决方案,那将很有帮助。

Try HashMap to store key and value pair. 尝试使用HashMap来存储键和值对。 In your question , store word to be replaced as a key ie "and" and value should be '&'. 在您的问题中,将要替换的单词存储为键,即“和”,值应为“&”。

create Set of vowels to check against whether it is vowel or not. 创建元音集以检查是否为元音。 Try below program. 请尝试以下程序。

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test {

private static final Map<String, Character> keyMap = new HashMap<String, Character>(); // store key and value
private static final Set<Character> vowelSet = new HashSet<Character>(); // store vowel in set

static {
    keyMap.put("and", '&');
    keyMap.put("to", '2');
    keyMap.put("you", 'u');
    keyMap.put("for", '4');

    vowelSet.add('a');
    vowelSet.add('e');
    vowelSet.add('i');
    vowelSet.add('o');
    vowelSet.add('u');
}

public static void main(String[] args) {
    System.out.println(shortHand("For I shall love you forever"));
}

public static String shortHand(String str) {
    StringBuffer result = new StringBuffer();
    String[] strArr = str.split(" "); //seperate string  by the space
    for (int i = 0; i < strArr.length; i++) {
        String temp = strArr[i].toLowerCase();
        if (keyMap.containsKey(temp)) {//check in map eg. does it contains in the map then replace it with shorthand word.
            result.append(keyMap.get(temp));
            result.append(" ");
        } else {
            if (temp.length() == 1 && vowelSet.contains(temp.charAt(0))) {//if it is only a vowel
                result.append(temp);
                result.append(" ");
            } else {
                for (int j = 0; j < temp.length(); j++) {//check in every character if its vowel or not
                    if (!vowelSet.contains(temp.charAt(j))) {
                        result.append(temp.charAt(j));
                    }
                }
                result.append(" ");
            }
        }

    }
    return result.toString();
}
}

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

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