Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的字符串比较_Java_String Comparison - Fatal编程技术网

java中的字符串比较

java中的字符串比较,java,string-comparison,Java,String Comparison,我们使用iTextSharp开发了一个PDF阅读器桌面应用程序,现在我们正在开发一个android应用程序。这是我的C代码,我想知道Java中的StringComparison可以使用什么 public final void PDFReferenceGetter(String pSearch, StringComparison SC, String sourceFile, String destinationFile) { // } 完整代码 public final void PDF

我们使用
iTextSharp
开发了一个PDF阅读器桌面应用程序,现在我们正在开发一个android应用程序。这是我的C代码,我想知道Java中的
StringComparison
可以使用什么

public final void PDFReferenceGetter(String pSearch, StringComparison SC, String sourceFile, String destinationFile)
{
    //
}
完整代码

public final void PDFReferenceGetter(String pSearch, String SC, String sourceFile, String destinationFile)
{
    PdfStamper stamper = null;
    PdfContentByte contentByte;
    Rectangle refRectangle = null;

    int refPage = 0;

    //this.Cursor = Cursors.WaitCursor;

    if ((new java.io.File(sourceFile)).isFile())
    {
        PdfReader pReader = new PdfReader(sourceFile);

        stamper = new PdfStamper(pReader, new FileOutputStream(destinationFile));

        for (int page = 1; page <= pReader.getNumberOfPages(); page++)
        {
            LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
            contentByte = stamper.getUnderContent(page);

            //Send some data contained in PdfContentByte, looks like the first is always cero for me and the second 100, but i'm not sure if this could change in some cases
            strategy._UndercontentCharacterSpacing = contentByte.getCharacterSpacing();
            strategy._UndercontentHorizontalScaling = contentByte.getHorizontalScaling();

            //It's not really needed to get the text back, but we have to call this line ALWAYS, 
            //because it triggers the process that will get all chunks from PDF into our strategy Object
            String currentText = PdfTextExtractor.getTextFromPage(pReader, page, strategy);

            //The real getter process starts in the following line
            java.util.ArrayList<Rectangle> matchesFound = strategy.GetTextLocations("References", SC);

            //Set the fill color of the shapes, I don't use a border because it would make the rect bigger
            //but maybe using a thin border could be a solution if you see the currect rect is not big enough to cover all the text it should cover
            contentByte.setColorFill(BaseColor.PINK);

            //MatchesFound contains all text with locations, so do whatever you want with it, this highlights them using PINK color:s
            for (Rectangle rect : matchesFound)
            {
                refRectangle = rect;
                refPage = page;
            }
            contentByte.fill();

        }

        for (int page = 1; page <= pReader.getNumberOfPages(); page++)
        {
            LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
            contentByte = stamper.getUnderContent(page);

            //Send some data contained in PdfContentByte, looks like the first is always cero for me and the second 100, but i'm not sure if this could change in some cases
            strategy._UndercontentCharacterSpacing = contentByte.getCharacterSpacing();
            strategy._UndercontentHorizontalScaling = contentByte.getHorizontalScaling();

            //It's not really needed to get the text back, but we have to call this line ALWAYS, 
            //because it triggers the process that will get all chunks from PDF into our strategy Object
            String currentText = PdfTextExtractor.getTextFromPage(pReader, page, strategy);

            String text    = currentText;
            String patternString = pSearch;
            Pattern pattern = Pattern.compile(patternString);

            Matcher matcher = pattern.matcher(text);
            boolean matches = matcher.matches();

            if(matches == true)
            {
            ArrayList<String> mc;
            mc.add(text);

            //MatchCollection mc = Regex.Matches(currentText, pSearch);
            java.util.ArrayList<Rectangle> matchesFound = new java.util.ArrayList<Rectangle>();
            for (String m : mc)
            {
                matchesFound = strategy.getTextLocations(m.toString(), SC);

                for (Rectangle rect : matchesFound)
                {
                    contentByte.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());

                    PdfDestination pdfdest = new PdfDestination(PdfDestination.XYZ, refRectangle.LEFT, refRectangle.TOP, 0);
                    PdfAnnotation annot = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT, refPage, pdfdest);
                    stamper.addAnnotation(annot, page);
                }
            }

            //The real getter process starts in the following line
            //Set the fill color of the shapes, I don't use a border because it would make the rect bigger
            //but maybe using a thin border could be a solution if you see the currect rect is not big enough to cover all the text it should cover
            contentByte.setColorFill(BaseColor.LIGHT_GRAY);

            //MatchesFound contains all text with locations, so do whatever you want with it, this highlights them using PINK color:

            contentByte.fill();

        }

        stamper.close();
        pReader.close();
    }
    //this.Cursor = Cursors.Default;
    }
}
public final void PDFReferenceGetter(字符串pSearch、字符串SC、字符串sourceFile、字符串destinationFile)
{
PdfStamper压模=null;
PdfContentByte contentByte;
矩形反射角=空;
int refPage=0;
//this.Cursor=Cursors.WaitCursor;
if((新java.io.File(sourceFile)).isFile())
{
PdfReader PREDER=新PdfReader(源文件);
stamper=新的PdfStamper(预订单、新文件输出流(destinationFile));

对于(int page=1;page,如果您有一个字符串:

String myString = "somestring";
还有一个:

String anotherString = "somestringelse";
您可以这样使用内置的
equals()
函数:

if(myString.equals(anotherString)) {
 //Do code
}

您可以使用基本的Java字符串比较方法

  • 如果你想完全比较两个字符串,我指的是整个字符串
  • 字符串string1=“abcd”,string2=“abcd”

    如果(string1.equals(string2))--->返回true,因为它们相等,否则返回false

  • 如果要比较两个字符串,完全忽略它们的大小写,可以使用以下方法
  • 字符串string1=“abcd”,string2=“abcd”

    如果(string1.equalsIgnorecase(string2))-->返回true,因为它们相等,尽管它们的大小写不同,否则返回false

    如果不想比较整个字符串,可以使用以下方法

    检查以下链接以了解Java中的所有字符串比较方法


    将更详细地描述
    StringComparison
    enum

    简而言之,不幸的是,java库中没有合适的类型

    简单的解决办法 创建自己的Java枚举镜像c#。您还必须创建自己的字符串比较方法,同时考虑
    StringComparison
    ,例如忽略大小写等,具体取决于
    StringComparison
    的值

    最佳解决方案
    我会避免在方法的接口中使用
    StringComparison
    。而是搜索该方法的用法。我猜它只是用于有时忽略大小写,而其他则不使用。或者它是完全未使用的。对于后一种情况,只需删除它,就可以了!对于前一种情况,只需将bool传递到接口instea即可d!记住更新c代码,以保持端口在某种程度上同步。

    可能的重复我已经添加了完整的代码,它搜索“References”一词并给出可以注释的itext矩形位置。感谢您的修复@ZerO。我想:\n您是否建议我为此目的创建另一个类?这取决于您尝试执行的操作。您想知道如何进行字符串比较,至少我从您的标题中得到了这一点。您应该从什么是“比较”。