0

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 (select 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

1

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

1 Comment

do you have any example maybe?
1

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;
            }
        }
    }
}

1 Comment

Boss!! he tagged this question as java,why answer in c#.Remove before you get downvotes..
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.