假设我有一个字符串列表,例如:
List myList = new ArrayList();
myList.add("person [something]");
myList.add("person 'something'");
所以在我的字符串中有时我有[,有时是'
现在我想获取子字符串,但是如果我想将字符串切成两个字符之一,我不知道该怎么做。 谁能告诉我该选项是否可行? 我尝试过类似的事情:
List finalOne = new ArrayList();
for(String ss: myList){
finalOne.add(ss.substring("[" || "'" ))
}
我希望一次运行就能从char“ [”或从“'”开始获取所有子字符串
答案 0 :(得分:3)
您可以使用<script>
// this is my loop variable for states //
var states = ['Alabama', 'Alaska', 'American Samoa', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Federated States of Micronesia', 'Florida', 'Georgia', 'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Marshall Islands', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Northern Mariana Islands', 'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Island', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
for (var s = 0; s < states.lenghth; s++); {
console.log(states);
for (t = 0; t <= 24; t += 1) {
console.log(t);
}
}
</script>
<select>
<option value ='00'>Choose your lift ticket time</option>
<option value ='0'>06:00 - 12:00</option>
<option value ='1'>12:00 - 18:00</option>
<option value ='2'>18:00 - 24:00</option>
</select>
<select>
<option value ='00'>Choose your State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<button onclick="calcPrice();">book</button>
和(?<=\[)(.*?)(?=\])
正则表达式在方括号和单引号之间找到字符串。
答案 1 :(得分:0)
这不是substring
方法的工作原理,请看文档(https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int))。
根据您的需要,您可以:
substring
方法获取它