在这篇文章中,我们将为您详细介绍字符串截取子串的内容,并且讨论关于Javasubstring,indexOf的相关问题。此外,我们还会涉及一些关于30.SubstringwithConcatenati
在这篇文章中,我们将为您详细介绍字符串截取子串的内容,并且讨论关于Java substring , indexOf的相关问题。此外,我们还会涉及一些关于30. Substring with Concatenation of All Words (JAVA)、76. Minimum Window Substring (JAVA)、c#substring indexof、C#中String类的几个方法(IndexOf、LastIndexOf、Substring)的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 字符串截取子串(Java substring , indexOf)(字符串截取子串的方法)
- 30. Substring with Concatenation of All Words (JAVA)
- 76. Minimum Window Substring (JAVA)
- c#substring indexof
- C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
字符串截取子串(Java substring , indexOf)(字符串截取子串的方法)
前言
因为之前java课设做的是股票分析系统,我找的接口返回的是一个.csv文件,因为这种文件里面的数据是以逗号分隔的,所以要对数据进行分析的时候需要截取子串,并且以逗号作为截取的标志。所以接下来就说一下我使用的字符串函数 substring和indexOf。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
字符串函数(字符串str为:2019-07-20-13-32)
substring函数的参数不同,功能也不一样。比如说一个参数的时候,给一个字符串里面的位置,然后从当前位置一直截取到字符串尾。比如temp=str.substring(5),那么temp=07-20-13-32。
如果有两个参数,那么截取的是两个位置之间的字符串。比如temp=str(0,3),那么temp=2019。
indexOf函数的表示方法是给一个字符,返回的是该字符串中第一个该字符的位置,比如说是str.indexOf(''-''),返回值就是4。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实例
package q;
import java.util.ArrayList;
public class Q {
public static void main(String[] args) {
String str="2019-07-20-11-54"; //初始字符串
ArrayList<String> list = new ArrayList<String>();
String[] st=new String[100];
int num=0;
String temp=null;
while(str!=null) { //解析每一行里面每一块的数据含义
num=str.indexOf(''-''); //根据字符串里面分隔的字符来选择 返回的是字符串中第一个该字符出现的位置 (例如.csv文件分隔符就是逗号)
if(num>=0) {
temp=str.substring(0,num); //截取出来的子串存到temp
list.add(temp); //先将子串放到list里面
str=str.substring(num+1); //再将剩下的字符串处理
}
else { //因为处理完最后一个分隔符后就找不到了 所以最后一个就是最后一个子串
list.add(str);
break;
}
}
for(int i=0;i<list.size();++i) {
st[i]=list.get(i); //再将子串放到数组中
}
for(int i=0;i<list.size();++i) {
System.out.println("st["+i+"]="+st[i]);
}
}
}
截取子串的结果
比如分隔符是逗号,
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
结语
放假了也不能颓废,菜鸡的挣扎,加油!
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
30. Substring with Concatenation of All Words (JAVA)
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
Example 1:
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> ret = new ArrayList<>();
if(words==null || words.length==0) return ret;
int start; //start index of s
int cur; //current index of s
Map<String, Integer> wordCnt= new HashMap<String, Integer>(); //count of each word
Map<String, Integer> strCnt= new HashMap<String, Integer>(); //count of each word while traversing string
String w;
String w_to_del;
int startIndex;
int cnt;
int len = words[0].length();
int strLen = words.length * len;
//initialize map
for(String str: words){
wordCnt.put(str,wordCnt.getOrDefault(str,0)+1);
}
for(int j = 0; j < len; j++){ //word可能会出现在s的任意位置,而非仅仅0,len,2*len...的位置
startIndex = j;
strCnt.clear();
for(int i = j; i <= s.length()-len; i+=len){
w = s.substring(i,i+len);
if(wordCnt.containsKey(w)){ //word in words
strCnt.put(w, strCnt.getOrDefault(w,0)+1);
//number of word in string is more than that in words array, move right startIndex
while(strCnt.get(w) > wordCnt.get(w)){
w_to_del = s.substring(startIndex,startIndex+len);
strCnt.put(w_to_del,strCnt.get(w_to_del)-1);
startIndex += len;
}
//find a match
if(i + len - startIndex == strLen){
ret.add(startIndex);
//start to find another match from startIndex+len
w_to_del = s.substring(startIndex,startIndex+len);
strCnt.put(w_to_del,strCnt.get(w_to_del)-1);
startIndex += len;
}
}
else{ //word not in words
startIndex = i+len;
strCnt.clear();
}
}
}
return ret;
}
}
时间复杂度:通过window的方式,在每个word的起始位置,只要遍历一遍s,就能完成查询。一共有size =words.length个起始位置,所以时间复杂度是O(n*size),其中n为s的长度,在s很长的时候,可以认为size是可忽略的常量,时间复杂度为O(n)。
76. Minimum Window Substring (JAVA)
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
""
. - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
重点:
- 滑动窗口
- Map 的 put,get,判断是否存在,遍历
- String 的截取
class Solution {
public String minWindow(String s, String t) {
Map<Character,Integer> target = new HashMap<Character,Integer>();
for(int i = 0; i < t.length(); i++){
if(!target.containsKey(t.charAt(i))) target.put(t.charAt(i),1);
else target.put(t.charAt(i), target.get(t.charAt(i))+1);
}
int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int minLeft = 0;
int minRight = s.length()-1;
Map<Character,Integer> source = new HashMap<Character,Integer>();
source.put(s.charAt(0),1);
while(left<=right){
if(ifContain(source,target)){
if(right-left+1 < minLength){
minLength = right-left+1;
minLeft = left;
minRight = right;
}
source.put(s.charAt(left), source.get(s.charAt(left))-1);
left++;
}
else{
right++;
if(right == s.length()) break;
if(!source.containsKey(s.charAt(right))) source.put(s.charAt(right),1);
else source.put(s.charAt(right), source.get(s.charAt(right))+1);
}
}
if(minLength==Integer.MAX_VALUE) return "";
else return s.substring(minLeft,minRight+1);
}
public Boolean ifContain(Map<Character, Integer> source, Map<Character, Integer> target){
for(Character key: target.keySet()){
if(!source.containsKey(key) || source.get(key) < target.get(key)) return false;
}
return true;
}
}
c#substring indexof
“FirstName || Sam LastName || Jones Address || 123 Main ST …”(另外100个值)
我想从整个字符串中找到Sam和Jones.
所以字符串firstname = originalstring.substring …等
有谁知道我怎么做到这一点?
附加 –
我想我忘了提几件事.
FirstName||Sam\r\n MiddleName||\r\n LastName||Jones\r\n ....
所以现在,如果我计算不会帮助我的字符数,原因可能需要更多的项目而不仅仅是名字和姓氏.
解决方法
string myString = "FirstName||Sam LastName||Jones Address||123 Main ST...";string pattern = @"FirstName\|\|(\w+) LastName\|\|(\w+) ";Match m = Regex.Match(myString,pattern);string firstName = m.Groups[1].Valuestring lastName = m.Groups[2].Value;
见其演示here.
C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
下面是小编 jb51.cc 通过网络收集整理的代码片段。
小编小编现在分享给大家,也给大家做个参考。
String.IndexOf
String.LastIndexOf
String.Substring
从此实例检索子字符串。 示例:
总结一下:
以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。
关于字符串截取子串和Java substring , indexOf的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于30. Substring with Concatenation of All Words (JAVA)、76. Minimum Window Substring (JAVA)、c#substring indexof、C#中String类的几个方法(IndexOf、LastIndexOf、Substring)的相关信息,请在本站寻找。
本文标签: