stringtokenizer

Split string with multiple delimiter including delimiters

允我心安 提交于 2021-02-07 18:16:34
问题 I would like to split a string using multiple character delimiters, but I also want to store delimiters. My delimiters are ()+-*/ So for example, if I had a string 26+78(12*23)-16 I want to get 26 + 78 ( 12 * 23 ) - 16 each line as a separate array element. I think you can not use split function to achieve this. However, my trial with string-tokenizer also failed. How can I achieve this? 回答1: I wouldn't answer if it wasn't saturday night here: String s1 = "26+78(12*23)-16"; for(String s: s1

Split string with multiple delimiter including delimiters

荒凉一梦 提交于 2021-02-07 18:16:29
问题 I would like to split a string using multiple character delimiters, but I also want to store delimiters. My delimiters are ()+-*/ So for example, if I had a string 26+78(12*23)-16 I want to get 26 + 78 ( 12 * 23 ) - 16 each line as a separate array element. I think you can not use split function to achieve this. However, my trial with string-tokenizer also failed. How can I achieve this? 回答1: I wouldn't answer if it wasn't saturday night here: String s1 = "26+78(12*23)-16"; for(String s: s1

Split string with multiple delimiter including delimiters

时光总嘲笑我的痴心妄想 提交于 2021-02-07 18:04:29
问题 I would like to split a string using multiple character delimiters, but I also want to store delimiters. My delimiters are ()+-*/ So for example, if I had a string 26+78(12*23)-16 I want to get 26 + 78 ( 12 * 23 ) - 16 each line as a separate array element. I think you can not use split function to achieve this. However, my trial with string-tokenizer also failed. How can I achieve this? 回答1: I wouldn't answer if it wasn't saturday night here: String s1 = "26+78(12*23)-16"; for(String s: s1

Converting string into array of ints ex String st = “1 2 3 4 5” into ar=[1,2,3,4,5]

只愿长相守 提交于 2021-02-04 21:39:54
问题 I am reading in a string, as an entire line of numbers, separated by spaces, ie ie 1 2 3 4 5 . I want to convert them into an array of integers, so that I can manipulate them. But this code doesn't work. It says incompatible types. String str = br.readLine(); int[] array = new int[4]; StringTokenizer tok = new StringTokenizer(str," ", true); boolean expectDelim = false; int i = 0; while (tok.hasMoreTokens()) { String token = tok.nextToken(); ar[i] = Integer.parseInt(token); i++; } 回答1: If you

Converting string into array of ints ex String st = “1 2 3 4 5” into ar=[1,2,3,4,5]

萝らか妹 提交于 2021-02-04 21:36:41
问题 I am reading in a string, as an entire line of numbers, separated by spaces, ie ie 1 2 3 4 5 . I want to convert them into an array of integers, so that I can manipulate them. But this code doesn't work. It says incompatible types. String str = br.readLine(); int[] array = new int[4]; StringTokenizer tok = new StringTokenizer(str," ", true); boolean expectDelim = false; int i = 0; while (tok.hasMoreTokens()) { String token = tok.nextToken(); ar[i] = Integer.parseInt(token); i++; } 回答1: If you

Tokenizing strings using regular expression in Javascript

时光怂恿深爱的人放手 提交于 2020-01-01 11:48:34
问题 Suppose I've a long string containing newlines and tabs as: var x = "This is a long string.\n\t This is another one on next line."; So how can we split this string into tokens, using regular expression? I don't want to use .split(' ') because I want to learn Javascript's Regex. A more complicated string could be this: var y = "This @is a #long $string. Alright, lets split this."; Now I want to extract only the valid words out of this string, without special characters, and punctuation, i.e I

Using multiple delimiters with StringTokenizer

那年仲夏 提交于 2020-01-01 08:03:27
问题 I'd like to know how I can use multiple delimiters with StringTokenizer in java. For example one of these !,*,/,^ will occur as a delimiter. Also there will only be one at a time. 回答1: Use the constructor with two arguments, where the second is the delimiters. StringTokenizer tokenizer = new StringTokenizer(yourString, "!*^/"); 回答2: You can use String.split() method because it takes regex as a parameter. You can specify Regex such that it can split the string based upon one of these

Arrangement of words in a sentence alphabetically

◇◆丶佛笑我妖孽 提交于 2019-12-26 14:01:00
问题 Suppose we're given a sentence. Then, how would we arrange the words alphabetically? For example, Sentence: Big Bang Theory is better than Two and a Half Men. Arranged Sentence: a and Bang better Big Half is Men than Theory Two. I was thinking of applying a loop through the alphabets and comparing it with the sentence, but I keep getting confused. I can't think of anything. Help! 回答1: Use a StringTokenizer to tokenize the sentence and fill the words in a list. A StringTokenizer object uses

Arrangement of words in a sentence alphabetically

ぃ、小莉子 提交于 2019-12-26 14:00:50
问题 Suppose we're given a sentence. Then, how would we arrange the words alphabetically? For example, Sentence: Big Bang Theory is better than Two and a Half Men. Arranged Sentence: a and Bang better Big Half is Men than Theory Two. I was thinking of applying a loop through the alphabets and comparing it with the sentence, but I keep getting confused. I can't think of anything. Help! 回答1: Use a StringTokenizer to tokenize the sentence and fill the words in a list. A StringTokenizer object uses

How to set delimiters for PTB tokenizer?

落爺英雄遲暮 提交于 2019-12-25 07:39:41
问题 I'm using StanfordCore NLP Library for my project.It uses PTB Tokenizer for tokenization.For a statement that goes like this- go to room no. #2145 or go to room no. *2145 tokenizer is splitting #2145 into two tokens: #,2145. Is there any way possible to set tokenizer so that it does't identify #,* like a delimiter? 回答1: A quick solution is to use this option: (command-line) -tokenize.whitespace (in Java code) props.setProperty("tokenize.whitespace", "true"); This will cause the tokenizer to