开发者

find substrings inside string

开发者 https://www.devze.com 2022-12-27 14:39 出处:网络
How can i find substrings inside string and then remember and delete it when i found it. EXAMPLE: select * from (select a.iid_organizacijske_enote,

How can i find substrings inside string and then remember and delete it when i found it.

EXAMPLE:

select * from (select a.iid_organizacijske_enote, 
       a.sifra_organizacijske_enote "Sifra OE", 
       a.naziv_organizacijske_enote "Naziv OE", 
       a.tip_organizacijske_enote "Tip OE" 

I would like to get all word inside " ", so

  • Sifra OE
  • Naziv OE
  • TIP OE

and return

select * from (se开发者_运维知识库lect a.iid_organizacijske_enote, 
       a.sifra_organizacijske_enote,
       a.naziv_organizacijske_enote, 
       a.tip_organizacijske_enote

i try with regex, indexOf() but no one works ok


String.replace(..):

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

str = str.replace(wordToRemove, "");

If you don't know the words in advance, you can use the regex version:

str = str.replaceAll("\"[^\"]+\"", "");

This means, that all strings starting and ending with quotes, with any character except quotes between them, will be replaced with empty string.


Consider using regex with capturing groups. With Java's Matcher class, you can find the first match, and then use replaceFirst(String).

--EDIT--

example (not efficient for long inputs):

String in = "hello \"there\", \"friend!\"";
Pattern p = Pattern.compile("\\\"([^\"]*)\\\"");
Matcher m = p.matcher(in);
while(m.find()){
    System.out.println(m.group(1));
    in = m.replaceFirst("");
    m = p.matcher(in);
}
System.out.println(in);


i tried and created function as below -- its working fine and returning output you want

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            string s =  p.mystring("select * from (select a.iid_organizacijske_enote, a.sifra_organizacijske_enote 'Sifra OE', "
        +"a.naziv_organizacijske_enote 'Naziv OE', "+
       "a.tip_organizacijske_enote 'Tip OE'");
        }


        public string mystring(string s)
        {
            if (s.IndexOf("'") > 0)
            {
                string test = s.Substring(0, s.IndexOf("'"));
                s = s.Replace(test+"'", "");

                s = s.Remove(0, s.IndexOf("'") + 1);
                test = test.Replace("'", "");
                test = test + s;
                return mystring(test);
            }
            else
            {
                return s;
            }
        }
    }
}


best & optimized code is here:

    public static void main(String[] args){
    int j =0;
    boolean substr = true;
    String mainStr = "abcdefgh";
    String ipStr = "efg";
    for(int i=0 ; i < mainStr.length();i++){
        if(j<ipStr.length() && mainStr.charAt(i)==ipStr.charAt(j)){
            j++;
        }               
    }
    if(j>=0 && j !=ipStr.length()){
        substr = false;
    }
    System.out.println("its a substring:"+substr);
}
0

精彩评论

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

关注公众号