如何在反斜杠处拆分java字符串

时间:2014-05-20 05:25:03

标签: java

String fname="C:\textfiles\db\query\query.txt";

这是我需要拆分它的字符串。

我试过这个

String [] items=fname.split("\");  

但不行。

String [] items=fname.split("\\"); also not working...

如何拆分此字符串......

9 个答案:

答案 0 :(得分:48)

首先,你没有发布问题的字符串

String fname="C:\textfiles\db\query\query.txt";

这应该被

取代
String fname="C:\\textfiles\\db\\query\\query.txt";

反斜杠(" \")也需要转义。

最后你需要做这样的事情来分割它们:

 String fname="C:\\textfiles\\db\\query\\query.txt";
 String[] items= fname.split("\\\\");
 System.out.println(Arrays.toString(items));

希望这有帮助。

答案 1 :(得分:9)

它有效。

String fname="C:\\textfiles\\db\\query\\query.txt";
       String split[]  = fname.split("\\\\");
       System.out.println(" :: value " + split[0] );

答案 2 :(得分:9)

'split'期待RegEx。 使用split的最佳方法是使用“Pattern.quote”

String separator = "\\";
String value = "C:\\Main\\text.txt";
String[] arrValues = value.split(Pattern.quote(separator));

答案 3 :(得分:1)

public static String[] splitPath (String path) {
    String backslash = ((char)92) + "";
    if (path.contains(backslash)) {
        ArrayList<String> parts = new ArrayList<>();
        int start = 0;
        int end = 0;
        for ( int c : path.toCharArray() ) {
            if (c == 92) {
                parts.add(path.substring(start, end));
                start = end + 1;
            }
            end++;
        }
        parts.add(path.substring(start));
        return parts.toArray( new String[parts.size()] );
    }
    return path.split("/");
}

答案 4 :(得分:1)

首先,您需要将所有出现的“ \”替换为“ \\”,然后使用“ \\”进行拆分。

String str="C:\Users\prajwal_nayak\Documents\queries.sql";
String separator = "\\";
String[] str_arr=str.replaceAll(Pattern.quote(separator), "\\\\").split("\\\\");

答案 5 :(得分:1)

请使用以下代码...

const uuidv1 = require('uuid/v1');

    module.exports = function (migration) {
      // create new content type
      const copycat = migration.createContentType('testDog').name('test-Dog').description('Test for cloning entries');

      copycat.createField('woofs', {
        name: 'woof',
        type: 'Symbol',
        required: true
      });
      copycat.createField('meow', {
        name: 'meow',
        type: 'Symbol',
        required: true
      });
      copycat.createField('dogObject', {
            name: 'dogObject',
            type: 'Object',
            localized: true
       });
      // add entry title
      copycat.displayField('woofs');

      migration.transformEntriesToType({
        sourceContentType: 'dog',
        targetContentType: 'testDog',
        from: ['woofs', 'meow', 'dogObject'],
        shouldPublish: false,
        updateReferences: true,
        removeOldEntries: false,
        identityKey: function () {
          const value = uuidv1().toString();
          return value
        }
           transformEntryForLocale:function(fromFields,currentLocale){
            return{
            woof:`${fromFields.woof[currentLocale]}`,
              meow:`${fromFields.meow[currentLocale]}`,
              dogObject:`${fromFields.dogObject[currentLocale]}`
            }
          }
      });
    };

答案 6 :(得分:0)

**//single slash already used as space in java 
// suppose it would be Object Type obj="C:\textfiles\db\query\query.txt";**

public static void main(String[] args) {
String target="\\";
String replacement="\\\\";
String str=(String) obj;
str=str.replace(target, replacement);
String[] strarray=str.split(replacement);
String filename=strarray[8];
System.out.println("file Name: "+filename);
}

答案 7 :(得分:0)

请使用以下代码...

String str[]=fname.split("\\\\\\\");

答案 8 :(得分:-2)

使用正则表达式转义字符。 &#34;字符串&#34; .split(&#34; \ /&#34);

相关问题