senzacionale
senzacionale

Reputation: 20936

find substrings inside string

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

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

Upvotes: 0

Views: 439

Answers (4)

user2862544
user2862544

Reputation: 425

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

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176956

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

Upvotes: 1

Eyal Schneider
Eyal Schneider

Reputation: 22456

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

Upvotes: 1

Bozho
Bozho

Reputation: 597402

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.

Upvotes: 3

Related Questions