如何在Java中按字符串的数据类型拆分字符串?

时间:2019-08-05 17:18:33

标签: java split

在我的Spigot插件中,玩家输入了诸如import UIKit class HeaderController { override init(frame: CGRect) { super.init(frame: frame) let categoryCollectionController = CategoryCollectionController(collectionViewLayout: UICollectionViewFlowLayout()) let categoryView = categoryCollectionController.view! categoryView.translatesAutoresizingMaskIntoConstraints = false addSubview(categoryView) NSLayoutConstraint.activate([ categoryView.leftAnchor.constraint(equalTo: leftAnchor), categoryView.rightAnchor.constraint(equalTo: rightAnchor), categoryView.topAnchor.constraint(equalTo: topAnchor), categoryView.bottomAnchor.constraint(equalTo: bottomAnchor) ]) } } class CategoryCollectionController: UICollectionViewController, UICollectionViewDelegateFlowLayout { let cellID = "UID" override func viewDidLoad() { super.viewDidLoad() collectionView.backgroundColor = .red collectionView.register(CellCategory.self, forCellWithReuseIdentifier: cellID) if let layout = collectionViewLayout as? UICollectionViewFlowLayout { layout.scrollDirection = .horizontal } } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! CellCategory return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 100, height: 100) } } 之类的命令。需要解析日期为/logging chat clear 1d的参数编号2(3rd arg)以作为日期(例如1d = 1天,15m = 15分钟)。我已经弄清楚了解析部分,但是当我尝试解析多个数字(1对11)时,我的解析不起作用,因为我是根据字符而不是整数或字符串进行拆分的。我做1d,然后将第一个字符作为数字,将第二个字符作为字符串。 如何做到这一点,但要拆分以便可以有多个号码? (正则表达式?)

3 个答案:

答案 0 :(得分:2)

private void myMethod() {
    String integers = "";
    String characters = "";
    String splitArgument = ""; //this is the 1d or 11d part
    for(int x = 0; x < splitArgument.length(); x++) {
        Char currentChar = splitArgument.charAt(x);
        if(Character.isDigit(currentChar)) {
            integers += currentChar;
        }else {
            characters += currentChar;
        }
    }
}

myMethod仅代表您要分析输入内容的代码区域。您可以创建一个方法(如isInteger()),该方法从要检查的字符串中获取字符并确定它们是否为Integers / String并重新输入-为它们连接字符串。对于整数部分,您可以执行以下操作:

int myInteger = Integer.parseInt(integers);

答案 1 :(得分:1)

使用正则表达式。

Pattern pattern = Pattern.compile("(\\d+)([a-zA-Z]+)");
Matcher matcher = pattern.matcher(text);
if(matcher.find()){
    String number = matcher.group(1);
    String letters = matcher.group(2);
}

答案 2 :(得分:0)

只需在date末尾的character参数始终为单个String的情况下执行此操作即可。

public static String[] splitDate(String date)
{
    int length = date.length();
    String[] results = {date.substring(0, length - 1), date.substring(length - 1)};
    return results;
}

输入110w将返回{110, w}的数组。

此方法仅根据substring的长度使用String,并将数字与最后一个字符分开。