开发者

Splitting string from the first occurrence of a character

开发者 https://www.devze.com 2023-03-08 20:31 出处:网络
I have multiple lines of text in log files in this kind of format: topic, this is the message开发者_开发知识库 part, with, occasional commas.

I have multiple lines of text in log files in this kind of format:

topic, this is the message开发者_开发知识库 part, with, occasional commas.

How can I split the string from the first comma so I would have the topic and the rest of the message in two different variables?

I've tried using this kind of split, but it doesn't work when there's more commas in the message part.

[topic, message] = whole_message.split(",", 2);


Use a regex that gets "everything but the first comma". So:

whole_message.match(/([^,]*),(.*)/)

[1] will be the topic, [2] will be the message.


That sort of decomposing assignment doesn't work in Javascript (at the present time). Try this:

var split = whole_message.split(',', 2);
var topic = split[0], message = split[1];

edit — ok so "split()" is kind-of broken; try this:

var topic, message;
whole_message.replace(/^([^,]*)(?:,(.*))?$/, function(_, t, m) {
  topic = t; message = m;
});


Here!

String.prototype.mySplit = function(char) { 
  var arr = new Array(); 
  arr[0] = this.substring(0, this.indexOf(char)); 
  arr[1] = this.substring(this.indexOf(char) + 1); 
  return arr; 
}

str = 'topic, this is the message part, with, occasional commas.'
str.mySplit(',');
-> ["topic", " this is the message part, with, occasional commas."]


javascript's String.split() method is broken (at least if you're expecting the same behavior that other language's split() methods provide).

An example of this behavior:

console.log('a,b,c'.split(',', 2))
> ['a', 'b']

and not

> ['a', 'b,c']

like you'd expect.

Try this split function instead:

function extended_split(str, separator, max) {
    var out = [], 
        index = 0,
        next;

    while (!max || out.length < max - 1 ) { 
        next = str.indexOf(separator, index);
        if (next === -1) {
            break;
        }
        out.push(str.substring(index, next));
        index = next + separator.length;
    }
    out.push(str.substring(index));
    return out;
};  


var a = whole_message.split(",");
var topic = a.splice (0,1);

(unless you like doing things complicated ways)


Why not split by comma, take the [0] item as topic then remove the topic(+,) from the original string ?

You could:

var topic = whole_message.split(",")[0]

(using prototype.js)

var message = whole_message.gsub(topic+", ", "") 

(using jQuery)

whole_message.replace(topic+", ", "")

Or quicker, go with josh.trow


let string="topic, this is the message part, with occasional commas."

let arr = new Array(); 
let idx = string.indexOf(',')
arr[0] = string.substring(0, idx);
arr[1] = string.substring(idx+1);
let topic = arr[0];
let message = arr[1]

output arr should be: ["topic", "this is the message part, with occasional commas."]

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号