如何在等号('=')之前删除所有String文本Java [英] How to remove all String text before equals ('=') sign Java

查看:89
本文介绍了如何在等号('=')之前删除所有String文本Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个字符串,以便构建一个XML文档.

I'd like to parse a string so I can build an XML document.

我有:

String value = "path=/Some/Xpath/Here";

我是这样解析的:

private void parseXpath() {
    String s = "path=/Some/Xpath/Here";

    String[] tokens = s.split("/");
    for(String t: tokens){
        System.out.println(t);
    }
}

输出:

path=
Some
Xpath
Here

如何完全删除"path =?我只想要xpath中的值?

How can I remove "path=" altogether? I want just the values from xpath?

感谢您的回复.

如何使此方法变为返回String []的方法,

How can I make this method into one which returns String[],

示例:

private String[] parseXpath(String s) {

String[] tokens = s.replaceFirst(".*=", "").split("/");
   for(String t: tokens){
      System.out.println(t);
   }
   return tokens;
}

我的输出是:

[Ljava.lang.String;@413849df

如何使其返回字符串数组?

How can I make it return me array of strings?

推荐答案

只需在拆分前进行替换.

Just do replace before splitting.

String[] tokens = s.replaceFirst(".*=", "").split("/");

起初这将为您提供一个空元素,因为它将在替换后的第一个正斜杠上进行分割.

This would give you an empty element at first because it would do splitting on the first forward slash after replacing.

String[] tokens = s.replaceFirst(".*=/", "").split("/");

但是,如果您删除直到 = 的所有字符以及第一个正斜杠,将为您提供所需的输出.

But if you remove all the chars upto the = along with the first forward slash will give you the desired output.

这篇关于如何在等号('=')之前删除所有String文本Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆