1

I have a set of strigs. String is made up of 4 words how can i get the whole string by comparing it with 3 words string.. planProductUniquekeySet contains 100% strings. and PlanProductUniqueKey is the 70% string, which i am comparing

for(String s:planProductUniquekeySet) {
             if(s.contains(PlanProductUniqueKey)){
                PlanProductUniqueKey=s;
            }
            }
        }

Its like i am comparing 70% with 100% of string and i want 100% string value at the end. Above code is giving 70% value at end.

If the string doesn't contain the exact contents of PlanProductUniqueKey, I want the entire string returned to me instead.

7
  • Is your criteria that it contain the 3 words and if it does you want the whole string? Your question is somewhat unclear to me. Also, do they need to be in the same order, at one end, or at the start?
    – crmprogdev
    May 30, 2016 at 15:19
  • @cromprogdeve Yes I want whole string back.. May 30, 2016 at 15:23
  • Could you answer my other questions? Do you already know the string contains 4 words for example? Does their sequence matter?
    – crmprogdev
    May 30, 2016 at 15:25
  • Yes Please, let me give you example, For example... 100% string is abc_def_ghi_jkl. And my 70 % string is abc_def_ghi...then i want full 100 % value.l May 30, 2016 at 15:29
  • What you just showed me doesn't contain words. They're both strings since there's no separation or white space.
    – crmprogdev
    May 30, 2016 at 15:30

3 Answers 3

1

as my understanding try this one

public class Test_one {
String PlanProductUniqueKey='apple bat cat';
    List<String> planProductUniquekeySet=null;
    public void execute()
    {
        planProductUniquekeySet= new List<String>();
        planProductUniquekeySet.add('dog apple bat cat');        
        planProductUniquekeySet.add('apple bat cat dog');
        for(String s:planProductUniquekeySet) 
        {
            if(s.contains(PlanProductUniqueKey))
            {
                System.debug('--- output---'+s);
            }
        }
    }
}

output is..

|DEBUG|--- output---dog apple bat cat
|DEBUG|--- output---apple bat cat dog
1
  • Hi, Prade! In this forum, please use "four spaces" to mark code as code (do not use the <code> tag). This allows the system to provide proper syntax highlighting and HTML escapes, as needed. You can use the "code" button in the editor to do this for you, or use the shortcut key Ctrl-K to add the necessary spaces.
    – sfdcfox
    May 31, 2016 at 3:09
1

You can use regex as well.

String strTotal = 'abc_def_ghi_jkl';
String strPercent = 'abc_def_ghi';
//Matches abc_def_ghi and anything after
String regExp = '(' + strPercent + '.*)';
String strResult = strTotal.replace(regExp, '');
System.debug(strResult);
1

You would need to have a compare string and a test string. The code you have now compares a string with itself. So, here's the kind of code you would need if you want to know if the compare string is contained in the test string.

string test = // from some source of text values
string compare = 'some string value';
if(test.contains(compare)){

    // you have a match, add your logic here...

}else{

   // no match, add your logic here...

}
2
  • There's no need for the == true in this code, though.
    – sfdcfox
    May 31, 2016 at 3:10
  • @sfdcfox Thanks for the catch. Updated the answer.
    – crmprogdev
    May 31, 2016 at 4:41

Not the answer you're looking for? Browse other questions tagged .